import requests
import xlwt
from lxml import etree as e
import numpy as py
import time
import matplotlib as chart
s=xlwt.Workbook()
sheet1=s.add_sheet("lucknum",cell_overwrite_ok=True)
targetdata=['开奖日期','期号','中奖号码1','中奖号码2','中奖号码3','中奖号码4','中奖号码5','中奖号码6','中奖号码7']
def get_data(url):
print(url)
headers={'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.49"}
res = requests.get(url,headers)
deal = e.HTML(res.text)
paths = deal.xpath("//tr")
print(paths)
date1=[]
num1=[]
qihao1=[]
for path in paths[2:-1]:
date1=path.xpath('//td[1]/text()')
num1=path.xpath('//td[3]/em/text()')
qihao1=path.xpath('//td[2]/text()')
return date1,num1,qihao1
date=['date']
qihao=['qihao']
num=['number']
for i in range(1,2):
urls1="http://kaijiang.zhcw.com/zhcw/html/ssq/list_"
urls2=str(i)
urls3=".html"
url=urls1+urls2+urls3
result=get_data(url)
date.append(result[0])
num.append(result[1])
qihao.append(result[2])
i += 1
time.sleep(0)
print(date)
print(num)
print(qihao)
for i in range(0,len(targetdata)):
sheet1.write(0,i,targetdata[i])
o1=0
for l1 in range(1,len(date)):
for k in range(0,len(date[l1])):
sheet1.write(o1+1,0,date[l1][k])
o1+=1
o1-=1
o2=0
for l2 in range(1,len(qihao)):
for j in range(0,len(qihao[l2])):
sheet1.write(o2+1,1,qihao[l2][j])
o2+=1
G=0
g=0
round=0
for l3 in range(1,len(num)):
while G<len(num[l3]):
if g<7:
sheet1.write(round + 1, g + 2, int(num[l3][G]))
G+=1
g+=1
else:
g=0
round+=1
l3+=1
G=0
print(round)
s.save("lottery-双色球.xls")
2021-05-13
最新推荐文章于 2023-09-14 01:09:05 发布
该博客内容涉及使用Python的requests、lxml和xlwt库爬取并存储双色球彩票的历史开奖数据。代码实现了从指定网址抓取数据,解析HTML,提取开奖日期、期号和中奖号码,并将这些信息写入Excel表格。
摘要由CSDN通过智能技术生成