Crawler:基于BeautifulSoup库+requests库+伪装浏览器的方式实现爬取14年所有的福彩网页的福彩3D相关信息,并将其保存到Excel表格中
生活随笔
收集整理的這篇文章主要介紹了
Crawler:基于BeautifulSoup库+requests库+伪装浏览器的方式实现爬取14年所有的福彩网页的福彩3D相关信息,并将其保存到Excel表格中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Crawler:Python爬取14年所有的福彩信息,利用requests庫和BeautifulSoup模塊來抓取中彩網頁福彩3D相關的信息,并將其保存到Excel表格中
?
?
目錄
輸出結果
核心代碼
?
?
?
輸出結果
中國彩票網
本來想做個科學預測,無奈,我看不懂爬到的數據……
得到數據:3D(爬取的14年所有的福彩信息).rar
好吧,等我看到了再用機器學習算法預測一下……
完整代碼,請點擊獲取
?
?
核心代碼
import requests import BeautifulSoup import xlwt import time#獲取第一頁的內容 def get_one_page(url):headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36'}response = requests.get(url,headers=headers)if response.status_code == 200:return response.textreturn None#解析第一頁內容,數據結構化 def parse_one_page(html):soup = BeautifulSoup(html,'lxml')i = 0for item in soup.select('tr')[2:-1]:yield{'time':item.select('td')[i].text,'issue':item.select('td')[i+1].text,'digits':item.select('td em')[0].text,'ten_digits':item.select('td em')[1].text,'hundred_digits':item.select('td em')[2].text,'single_selection':item.select('td')[i+3].text,'group_selection_3':item.select('td')[i+4].text,'group_selection_6':item.select('td')[i+5].text,'sales':item.select('td')[i+6].text,'return_rates':item.select('td')[i+7].text}#將數據寫入Excel表格中 def write_to_excel():f = xlwt.Workbook() sheet1 = f.add_sheet('3D',cell_overwrite_ok=True)row0 = ["開獎日期","期號","個位數","十位數","百位數","單數","組選3","組選6","銷售額","返獎比例"]#寫入第一行for j in range(0,len(row0)):sheet1.write(0,j,row0[j])#依次爬取每一頁內容的每一期信息,并將其依次寫入Exceli=0for k in range(1,247):url = 'http://kaijiang.zhcw.com/zhcw/html/3d/list_%s.html' %(str(k)) ……f.save('3D.xls')def main():write_to_excel()if __name__ == '__main__':main()?
總結
以上是生活随笔為你收集整理的Crawler:基于BeautifulSoup库+requests库+伪装浏览器的方式实现爬取14年所有的福彩网页的福彩3D相关信息,并将其保存到Excel表格中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成功解决Command quot;pyt
- 下一篇: Crawler:基于splinter.b