Python 获取网络数据插入到本地数据【sqlite】
1 import requests 2 import sqlite3 3 4 start_row = 0 5 end_row = 10000 6 conn = sqlite3.connect('ball.db') 7 cur = conn.cursor() 8 9 url = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize=3000&week=&systemType=PC" 10 11 headers = { 12 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36', 13 'Cookie': 'HMF_CI=85ab0f828f7e7329dec27ecd17cf4fdd3b592931c96b6289a31ac0c6cd0b73659256a82d0cbb6a2e2374cfe6aaee630c72ff9f87705d110ac7ed4200b534cb35a9; 21_vq=8' 14 } 15 16 response = requests.get(url=url, headers=headers) 17 info_list = response.json()['result'] 18 print(info_list) 19 print(type(info_list)) 20 21 print("<<<<<<<<<<<<<<<<<<<<<----------开始插入数据--------->>>>>>>>>>>>>>>>>>>") 22 23 for index in info_list: 24 number = index['code'] 25 week = index['date'] 26 red = index['red'] 27 blue = index['blue'] 28 29 str_split = str(red).split(",") 30 to_db = [number, str_split[0], str_split[1], str_split[2], str_split[3], str_split[4], str_split[5], blue, week, 0] 31 32 insert_query = "insert into lottery_new(datetime,red_ball_1,red_ball_2,red_ball_3,red_ball_4,red_ball_5,red_ball_6,blue_ball,remark,pid) values(?,?,?,?,?,?,?,?,?,?)" 33 cur.execute(insert_query, to_db) 34 35 conn.commit() 36 37 print("<<<<<<<<<<<<<<<<<<<<<----------执行完成---------->>>>>>>>>>>>>>>>>>>") 38