简单的获取大乐透历史数据

查看 65|回复 2
作者:ALDShD   
简单的获取大乐透历史数据[Python] 纯文本查看 复制代码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)
if __name__ == '__main__':
    historyData()


image.png (65.46 KB, 下载次数: 0)
下载附件
2023-3-25 11:43 上传

历史数据, 大乐透

alex999   

帮你改成了写入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()
ALDShD
OP
  


alex999 发表于 2023-3-25 11:58
帮你改成了写入mysql
[mw_shl_code=python,true]import pymysql
import requests

我准备写入 sqlite 数据库,这样可以单击使用了。
您需要登录后才可以回帖 登录 | 立即注册

返回顶部