帮你改成了写入mysql
[Python] 纯文本查看 复制代码 import pymysql
import requests
def historyData():
h_data_list = []
for i in range(1, 81): # for循环,一共81页;
url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=30&isVerify=1&pageNo=%s' % i
# print(url)
# html = get_url(url) # 请求url获取返回代码
historydata = requests.get(url).json()
historydata_list = historydata['value']['list']
# print(historydata_list)
for j in historydata_list:
lotteryDrawNum = j["lotteryDrawNum"]
lotteryDrawResult = j["lotteryDrawResult"]
lotteryDrawTime = j["lotteryDrawTime"]
h_data_list.append([lotteryDrawNum, lotteryDrawResult, lotteryDrawTime])
# print(lotteryDrawNum,":",lotteryDrawResult,":",lotteryDrawTime)
# print(h_data_list)
h_data_list = sorted(h_data_list, key=lambda x: x[0]) # 从小到大排序
print(h_data_list)
# Connect to the database
conn = pymysql.connect(host='localhost', user='root', password='root', db='lottery')
cursor = conn.cursor()
# Create table
cursor.execute('''CREATE TABLE IF NOT EXISTS lottery_history
(id INT PRIMARY KEY AUTO_INCREMENT,
lotteryDrawNum VARCHAR(20),
lotteryDrawResult VARCHAR(20),
lotteryDrawTime VARCHAR(20))''')
# Insert data into table
for data in h_data_list:
cursor.execute('''INSERT INTO lottery_history (lotteryDrawNum, lotteryDrawResult, lotteryDrawTime)
VALUES (%s, %s, %s)''', (data[0], data[1], data[2]))
# Commit changes and close connection
conn.commit()
cursor.close()
conn.close()
if __name__ == '__main__':
historyData()
|