import requests
import csv
import pprint
f = open('双色球.csv',mode='a',encoding='utf-8',newline='')
csv_writer = csv.DictWriter(f,fieldnames=['期号','开奖日期','红球','蓝球',
'一等奖中奖注数','一等奖中奖金额','二等奖中奖注数','二等奖中奖金额','三等奖中奖注数','三等奖中奖金额',
'四等奖中奖注数','四等奖中奖金额','五等奖中奖注数','五等奖中奖金额','六等奖中奖注数','六等奖中奖金额','七等奖中奖注数',
'七等奖中奖金额','一等奖中将地区','奖金金额'])
csv_writer.writeheader()
for page in range(1,56):
url = 'https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?'
params = {
'name': 'ssq',
'pageNo': page,
'pageSize': '30',
'systemType': 'PC'
}
headers = {'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
}
response = requests.get(url=url,params=params,headers=headers)
content = response.json()
# pprint.pprint(content)
for index in content['result']:
dit = {
'期号': index['code'],
'开奖日期':index['date'],
'红球':index['red'],
'蓝球': index['blue'],
'一等奖中奖注数': index['prizegrades'][0]['typenum'],
'一等奖中奖金额': index['prizegrades'][0]['typemoney'],
'二等奖中奖注数': index['prizegrades'][1]['typenum'],
'二等奖中奖金额': index['prizegrades'][1]['typemoney'],
'三等奖中奖注数': index['prizegrades'][2]['typenum'],
'三等奖中奖金额': index['prizegrades'][2]['typemoney'],
'四等奖中奖注数': index['prizegrades'][3]['typenum'],
'四等奖中奖金额': index['prizegrades'][3]['typemoney'],
'五等奖中奖注数': index['prizegrades'][4]['typenum'],
'五等奖中奖金额': index['prizegrades'][4]['typemoney'],
'六等奖中奖注数': index['prizegrades'][5]['typemoney'],
'六等奖中奖金额': index['prizegrades'][5]['typemoney'],
'七等奖中奖注数': index['prizegrades'][6]['typemoney'],
'七等奖中奖金额': index['prizegrades'][6]['typemoney'],
'一等奖中将地区':index['content'],
'奖金金额':index['poolmoney']
}
csv_writer.writerow(dit)
'''安装数据编辑器
在终端 pip install jupyter notebook'''
更换个headers即可,亲测有效,但只能爬取前九页的信息,可能是遇到了反爬。