爬虫,抓取排列5中奖纪录

老爸最近迷上了彩票,要我下载排列5的中奖数据给他分析。
虽然知道没用,但是做不通他的工作啊。只能按他的要求抓取中奖纪录,让他去分析吧,也算是退休之后找点乐子。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
from bs4 import BeautifulSoup

lottery_file = open("sort5.txt", "wb")

for history_index in range(1, 237):
    webstr = 'http://www.lottery.gov.cn/historykj/history_%d.jspx?_ltype=plw' % history_index
#    print(webstr.encode('utf-8'))

    response = urllib.request.urlopen(webstr)
    soup = BeautifulSoup(response, "html.parser")
    response.close()
    tables = soup.findAll('table')
    tab = tables[0]

    row=0
    for tr in tab.findAll('tr'):
        column = 0
        row=row+1
        if(row == 1 or row == 2):
            continue
        for td in tr.findAll('td'):
            if(column == 0 or column == 1 or column == 7):
                lotteryvalue = td.getText()+'\t'
                lottery_file.write(lotteryvalue.encode('utf-8'))
                #print(td.getText())
            column = column+1
        lottery_file.write("\n".encode('utf-8'))

lottery_file.close()
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值