欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python抓取往期双色球

程序员文章站 2022-12-22 08:02:17
1 import requests 2 import json,csv 3 4 5 ''' 6 http://m.zhcw.com/clienth5.do?lottery=FC_SSQ&pageSize=20&pageNo=1&transactionType=300301&src=000010000... ......
 1 import requests
 2 import json,csv
 3 
 4 
 5 '''
 6 http://m.zhcw.com/clienth5.do?lottery=fc_ssq&pagesize=20&pageno=1&transactiontype=300301&src=0000100001%7c6000003060
 7 http://m.zhcw.com/clienth5.do?lottery=fc_ssq&pagesize=20&pageno=3&transactiontype=300301&src=0000100001%7c6000003060
 8 http://m.zhcw.com/clienth5.do?lottery=fc_ssq&pagesize=20&pageno=13&transactiontype=300301&src=0000100001%7c6000003060
 9 
10 '''
11 def get_url():
12     #拼接地址
13     for page in range(1,20):
14         base_url ='http://m.zhcw.com/clienth5.do?lottery=fc_ssq&pagesize=20&pageno='+str(page)+'&transactiontype=300301&src=0000100001%7c6000003060'
15 
16         #请求页面
17         response=requests.get(base_url)
18         data =json.loads(response.text)
19         datalist =data['datalist']
20 
21         #遍历每个期数
22         for dl in datalist:
23             kjissue=dl['kjissue']
24             kjdate=dl['kjdate']
25             kjznum=dl['kjznum']
26             kitnum=dl['kjtnum']
27 
28             #将json数据存储为csv
29             with open('./shuangseqiu.csv', 'a+', encoding='utf-8') as f:
30                 csv_write = csv.writer(f)
31                 rows=[kjissue,kjdate,kjznum,kitnum]
32                 csv_write.writerow(rows)
33 
34                 # print(kjissue,kjdate,kjznum,kitnum)
35 
36 def main():
37     get_url()
38 
39 if __name__ =="__main__":
40     main()