python双色球-(一)采集双色球历史数据

多年之后,再次感受双色球的魅力,芸芸众生都为之倾倒,但是温馨提示:赌博有风险,本文仅做数据处理技术讨论,不构成任何资金投入建议!

现如今的IT行业竞争越来越激烈,一线大厂都是各种裁员,IT35定律不说是绝对,但是市场的反应很现实也很真实,那就是不管你是不是35以上,岗位都在缩减,有人IT转了快递外卖小哥,有的转了煎饼师傅,有的转了滴滴师傅,惨烈的状况到处可见,所以买彩票的人更多了,为了一探究竟决定再次研究一下这神秘的五百万头奖,先从数据采集开始,详细代码如下,送给需要改变现状的我们!

import requests  # 发送请求
from bs4 import BeautifulSoup  # 解析网页
import pandas as pd  # 存取csv
from time import sleep  # 等待时间
from lxml import etree # 导入xpath包
import openpyxl,random

# 加载工作薄
wb = openpyxl.Workbook()
# 如果不存在即创建
wb.save(r'DoubleBall.xlsx')

# 打开excel工作簿
wb = openpyxl.load_workbook(r'DoubleBall.xlsx')
# 准备写工作簿中第一个sheet页
sheet1 = wb.worksheets[0]

# 为数据页循环标识
indexnum = 1
print('爬取开始了')
#http://kaijiang.zhcw.com/zhcw/html/ssq/list_2.html
# 初始化URL第一页地址
url = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list.html'
# 请求头部,伪造浏览器,防止爬虫被反
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
}
# 定义excel中的行数
indexcount = 0
# 初步了解数据总页数为151,可以放更大的值
while indexnum < 152:
    #如果不是数据页第一页则拼接数据页地址
    if indexnum > 1:
        url = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list_' + str(indexnum) + '.html'

    # 利用请求地址和请求头部构造请求对象
    res = requests.get(url, headers=headers)
    # BeautifulSoup解析包
    soup = BeautifulSoup(res.text, 'html.parser')
    # xpath解析包 构造 _Element 对象
    res_elements = etree.HTML(res.text)
    
    # 使用 xpath 匹配数据,找到页面的table列表标签 ,
    table = res_elements.xpath('//table')
    # 函数可以将转换为Element对象再转换回html字符串
    table = etree.tostring(table[0], encoding='utf-8').decode()
    # read_html()函数是将HTML的表格转换为DataFrame
    df = pd.read_html(table, encoding='utf-8', header=0)[0]
    # 将dataframe进行转置,进而用to_dict()方法转为字典类型
    results = list(df.T.to_dict().values())

    # 根据每页数据条数,控制循环次数
    count = 1    
    # 循环取每页的数据
    for index in results:
        # 只有第一页和非第一页且不是第一条数据需要取出来存放到excel(因为每页都有表头)
        if indexnum == 1 or (indexnum != 1 and count != 1):
            # 如果一页的数据循环到最后一条时退出,跳转到下一页
            if count < len(results):
                # 定义并赋值数据所需要存储的行号
                indexcount = (indexnum - 1)*20 + count
                sheet1.cell(indexcount,1,index.get('开奖日期'))
                sheet1.cell(indexcount,2,index.get('期号'))
                # 由于所有号码都是存在一个值当中,所以需要通过按空格分隔为list
                listball = index.get('中奖号码').split()
                # 判断数据是否第一行是否为表头,如果大于1就不是,则动态赋值,如果是则直接写文字内容
                if len(listball) > 1:
                    sheet1.cell(indexcount,3,listball[0])
                    sheet1.cell(indexcount,4,listball[1])
                    sheet1.cell(indexcount,5,listball[2])
                    sheet1.cell(indexcount,6,listball[3])
                    sheet1.cell(indexcount,7,listball[4])
                    sheet1.cell(indexcount,8,listball[5])
                    sheet1.cell(indexcount,9,listball[6])
                else:
                    sheet1.cell(indexcount,3,'红球1')
                    sheet1.cell(indexcount,4,'红球2')
                    sheet1.cell(indexcount,5,'红球3')
                    sheet1.cell(indexcount,6,'红球4')
                    sheet1.cell(indexcount,7,'红球5')
                    sheet1.cell(indexcount,8,'红球6')
                    sheet1.cell(indexcount,9,'蓝球')
                    
                sheet1.cell(indexcount,10,index.get('中奖注数'))
                sheet1.cell(indexcount,11,index.get('中奖注数.1'))
        
                
        count+=1
            
    indexnum+=1

# 保存关闭excel
wb.save(r'DoubleBall.xlsx')
print('爬取结束了')

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值