from bs4 import BeautifulSoup
import requests
import re
url='http://www.17500.cn/ssq/awardlist.php'
res=requests.get(url)
cont=BeautifulSoup(res.text)
all={}
for i in cont.body.tbody.find_all('tr'):
tds=i.find_all('td')
if '2020' not in tds[0].text:
break
all[tds[0].text]=re.split(r'[ +]',tds[3].text)
#print(all)
def panduan(zixuan):
zixuan=re.split(r'[ +]',zixuan)
for key in all:
blue=all[key][-1]==zixuan[-1]
red=0
for i in zixuan[:-1]:
if i in all[key][:-1]:
red+=1
if red<=2 and blue:
print(key,"六等奖")
elif red+blue==4:
print(key,"五等奖")
elif red+blue==5:
print(key,"四等奖")
elif red==5 and blue:
print(key,"三等奖")
elif red==6 and not blue:
print(key,"二等奖")
elif red==6 and blue:
print(key,"一等奖")
else:
print(key,"未中奖")
zixuan=['01 03 25 26 28 33+08','01 03 25 26 28 33+01','01 03 25 26 28 33+09']
for i in zixuan:
panduan(i)
建议将爬取的结果保存到本地文件,这样之后不用每次都去网上爬取结果
本代码只是实现了功能,代码没有做过多的优化,不过思路应该就是这样的
一下修改后的代码,只提取当月数据,细算号码放到一个列表里,大致框架都一样,集体你可以自己调整
from bs4 import BeautifulSoup
import requests
import re
from datetime import datetime
url='http://www.17500.cn/ssq/awardlist.php'
res=requests.get(url)
cont=BeautifulSoup(res.text)
all={}
#只提取当月数据
date=datetime.today()
date="{:d}-{:02d}".format(date.year,date.month)
#print(date)
for i in cont.body.tbody.find_all('tr'):
tds=i.find_all('td')
if date not in tds[1].text:
break
all[tds[0].text]=re.split(r'[ +]',tds[3].text)
print(all)
def panduan(zixuan):
zixuan=re.split(r'[ +]',zixuan)
for key in all:
blue=all[key][-1]==zixuan[-1]
red=0
for i in zixuan[:-1]:
if i in all[key][:-1]:
red+=1
if red<=2 and blue:
print(key,"六等奖")
elif red+blue==4:
print(key,"五等奖")
elif red+blue==5:
print(key,"四等奖")
elif red==5 and blue:
print(key,"三等奖")
elif red==6 and not blue:
print(key,"二等奖")
elif red==6 and blue:
print(key,"一等奖")
else:
print(key,"未中奖")
#自选的号码,放到一个列表里
zixuan=['01 03 25 26 28 33+08','01 03 25 26 28 33+01','01 03 25 26 28 33+09']
for i in zixuan:
panduan(i)