本帖最后由 wd4748 于 2023-4-4 13:59 编辑
[Python] 纯文本查看 复制代码 import requests,json
import pymysql
headers ={
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'
}
data ={
"limit":20,
"page":1,
"params":{}
}
resp = requests.post('https://ms.zhcw.com/proxy/lottery-chart-center/history/SSQ',headers=headers,data=json.dumps(data))
result = json.loads(resp.text)['datas']
def historyData():
h_data_list = []
for i in range(len(result)):
issue = result[i]['issue']
openDate = result[i]['openDate']
week = result[i]['week']
winningFrontNum = result[i]['winningFrontNum']
winningBackNum = result[i]['winningBackNum']
h_data_list.append([issue, openDate, week,winningFrontNum,winningBackNum])
h_data_list = sorted(h_data_list, key=lambda x: x[0]) # 从小到大排序
print(h_data_list)
# 连接数据库
conn = pymysql.connect(host='****', user='root', password='***', db='mysql')
cursor = conn.cursor()
# 创建表
cursor.execute('''CREATE TABLE IF NOT EXISTS ssq_lottery_history
(id INT PRIMARY KEY AUTO_INCREMENT,
issue VARCHAR(20),
openDate VARCHAR(20),
week VARCHAR(20),
winningFrontNum VARCHAR(20),
winningBackNum VARCHAR(20)
)''')
# 向表中插入数据
for item in h_data_list:
print(item[0] + ' '+item[1] + ' '+item[2] + ' '+item[3] + '-'+item[4])
cursor.execute('''INSERT INTO ssq_lottery_history (issue, openDate, week,winningFrontNum,winningBackNum)
VALUES (%s, %s, %s, %s, %s)''', (item[0], item[1], item[2], item[3], item[4]))
print("存入数据库成功")
# 提交数据并关闭连接
conn.commit()
cursor.close()
conn.close()
if __name__ == '__main__':
historyData()
|