欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > HTML >内容正文

HTML

Crawler:基于BeautifulSoup库+requests库+伪装浏览器的方式实现爬取14年所有的福彩网页的福彩3D相关信息,并将其保存到Excel表格中

发布时间:2025/3/21 HTML 73 豆豆

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表格中的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。