ALDShD 发表于 2023-3-25 11:43

alex999 发表于 2023-3-25 11:58

帮你改成了写入mysql {:1_918:}
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()
            # print(lotteryDrawNum,":",lotteryDrawResult,":",lotteryDrawTime)
    # print(h_data_list)
    h_data_list = sorted(h_data_list, key=lambda x: x)# 从小到大排序
    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, data, data))

    # Commit changes and close connection
    conn.commit()
    cursor.close()
    conn.close()


if __name__ == '__main__':
    historyData()

ALDShD 发表于 2023-3-25 20:35

ALDShD 发表于 2023-3-25 12:00

ALDShD 发表于 2023-3-25 12:01

huangdanhuakui 发表于 2023-3-25 12:49

谢谢大佬分享

xihualing888 发表于 2023-3-25 13:24

{:1_907:}我是小白一个 不知道怎么用啊

ALDShD 发表于 2023-3-25 13:31

xihualing888 发表于 2023-3-25 13:32

ALDShD 发表于 2023-3-25 13:31
等我做个有界面的出来,你就会用了

谢谢 理解

lx19960103 发表于 2023-3-25 13:38

谢谢大佬分享,等我中奖!

ct268gh 发表于 2023-3-25 14:30

alex999 发表于 2023-3-25 11:58
帮你改成了写入mysql
import pymysql
import requests


请教大佬, 多次执行可以避免重复插入数据吗?
页: [1] 2 3 4
查看完整版本: 简单的获取大乐透历史数据