python用requests和bs4爬取10年大乐透中奖号码并存入Excel

爬取09年到目前的大乐透中奖号码,没有什么反爬手段,直接上代码开搞!

import requests
from bs4 import BeautifulSoup
import pandas as pd
url='http://datachart.500.com/dlt/history/newinc/history.php?start=09001&end=19024'
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
}
response=requests.get(url=url,headers=headers)
response.encoding=response.apparent_encoding
soap=BeautifulSoup(response.text,'lxml')
fir=soap.select('#tdata')[0]
sec=fir.find_all('tr')
writer=pd.ExcelWriter('cp.xlsx')
datas=[]
for i in sec:
    tds=i.find_all('td')
    l=[]
    for td in tds[0:8]:
        l.append(td.text)
    data={'期号':l[0],'前区01':l[1],'前区02':l[2],'前区03':l[3],'前区04':l[4],'前区05':l[5],'后区01':l[6],'后区02':l[7]}
    datas.append(data)
pd.DataFrame(datas).to_excel(writer,index=False)
writer.save()

你可能感兴趣的:(python用requests和bs4爬取10年大乐透中奖号码并存入Excel)