Python爬取大乐透

因为笔者经常投彩票,于是做了一个玩,初版本只是爬取,效果如下:
在这里插入图片描述
代码如下:

import requests
from requests.exceptions import RequestException
from lxml import etree
count = 0
def get_page(url):
    try:
        headers = {
            'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Mobile Safari/537.36',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'max-age=0',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
        }
        response = requests.get(url=url,headers=headers)
        # 更改编码方式,否则会出现乱码的情况
        response.encoding = "utf-8"
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None
def parse_page(html):
    try:
        global count
        count+=1
        res = etree.HTML(html)
        red = res.xpath('//*[@class="smallRedball"]//text()')
        blue = res.xpath('//*[@class="smallBlueball"]//text()')
        print(red+blue)
    except Exception as e:
        pass
def main(num):
    url = 'http://caipiao.163.com/t/award/dlt/{}.html'.format(str(num))
    html = get_page(url)
    parse_page(html)
if __name__ == '__main__':
    for num in range(17001,17154):
        main(num)
    for num in range(18001, 18140):
        main(num)
    print("大乐透爬取结束...")
    print("爬取的数目为:"+str(count))
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

精神抖擞王大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值