需求:
1、爬取2020年双色球开奖结果;
2、随机生成5注自选号码;
3、统计每一期中奖情况;
要求:python实现,代码简洁(同时能很好的解决问题)
例:
开奖结果
20001期: 01,03,25,26,28,33 | 08
20002期:。。。。。。。。。。。。。。。
20003期:。。。。。。。。。。。。。。。
自选1:01,08,25,26,28,33 | 05
自选2:。。。。。。。。。。。。。。。
自选3:。。。。。。。。。。。。。。。
中奖情况展示:
20001期 自选1 ‘’三等奖“
20001期 自选2 ‘’未中奖“
。。。。。。。。。。。。
中奖展示可以用其他形式,看起来清晰就好
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)
import requests
import re
#"http://kaijiang.500.com/shtml/ssq/19036.shtml"
def getInfo(url):
r = requests.get(url)
r.encoding = 'gbk'
res = r.text
no = "".join(re.findall(r'<font class="cfont2"><strong>(.+?)</strong>',res))
red = ",".join(re.findall(r'<li class="ball_red">(.+?)</li>',res))
blue = "".join(re.findall(r'<li class="ball_blue">(.+?)</li>',res))
res = "第" + no + "期:" + red + " "+blue
print(res)
for i in range(19001,19040):
url = "http://kaijiang.500.com/shtml/ssq/"+str(i)+".shtml"
getInfo(url)
https://blog.csdn.net/jerrycoding666/article/details/105420210