# 爬取大乐透的开奖历史数据
# http://www.lottery.gov.cn/api/lottery_kj_detail_new.jspx?_ltype=4&_term=19026
import requests
import re
import csv
agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
headers = {
'User-Agent': agent,
}
proxy={
"http":"125.39.9.34:9000",
}
url = 'http://www.lottery.gov.cn/api/lottery_kj_detail_new.jspx'
start = int(input('输入开始期号:')) # 18134
end = int(input('输入结束期号:')) # 19029
lottery_li = []
for qihao in range(start,end+1):
data={
'_ltype':'4',
'_term':qihao,
}
page_text = requests.post(url=url,headers=headers,data=data,proxies=proxy).text
print(page_text)
if page_text:
# 根据返回数据解析
# 开奖号码
lottery_data = re.findall('codeNumber\"\:\[(.*?)\],\"',page_text,re.M)
if lottery_data:
num_data = lottery_data[0].replace("\"",'')
# print(num_data) # 10,12,15,17,19,02,03
lottery_list = num_data.split(',')
lottery_list.insert(0,qihao)
# print(lottery_list) # ['10', '12', '15', '17', '19', '02', '03']
lottery_li.append(lottery_list)
with open('lottery_data.csv','w',newline='') as csvf:
spanwriter=csv.writer(csvf,dialect='excel') #创建writer对象
spanwriter.writerow(['qihao','red1','red2','red3','red4','red5','blue1','blue2']) #使用writer的方法writerow写入到文件
spanwriter.writerows(lottery_li) #迭代写入数据
print('done.....................')