Python學(xué)習(xí):用requests-html抓彩票數(shù)據(jù)

最近看到requests 作者 kennethreitz 出了一個(gè)新庫(kù) requests-html,拿來(lái)練練手。該庫(kù)旨在盡可能簡(jiǎn)單直觀地解析html(例如,抓取網(wǎng)頁(yè))。

官方文檔
http://html.python-requests.org/

來(lái)抓抓網(wǎng)易11選5的彩票的數(shù)據(jù)。
首先我們打開(kāi)網(wǎng)站,打開(kāi)開(kāi)發(fā)者工具找到對(duì)應(yīng)的html。

image
session = HTMLSession()

def getData():
    response = session.get('http://caipiao.163.com/award/11xuan5/')
    content = response.html.find('section.main', first=True)
    body = content.find('tbody')
    itemDicts = dict()
    for tr in body:
        list = tr.find('td.start')
        for td in list:
            try:
                period = td.attrs['data-period']
                award = td.attrs['data-award']
                print("序號(hào):" + td.text + " 期號(hào):" + period + " 開(kāi)獎(jiǎng)號(hào)碼:" + award)
                itemDicts[period] = award
            except KeyError as e:
                print('except: ', e)
            finally:
                print('finally')

因?yàn)檫€有沒(méi)有開(kāi)出來(lái)的開(kāi)獎(jiǎng)號(hào)碼 我們就try...except了。我們發(fā)現(xiàn)網(wǎng)頁(yè)是表格的,我們需要按期號(hào)排列。

sortItemDict = sorted(itemDicts.keys(), reverse=False)
    # print(sortItemDict)
    for key in sortItemDict:
        print("期號(hào):", key, " 開(kāi)獎(jiǎng)號(hào)碼:", itemDicts[key])

最后結(jié)果:


image

完整代碼(發(fā)現(xiàn)省了不少事,直接find元素s)

from requests_html import HTMLSession
import requests

session = HTMLSession()


def getData():
    response = session.get('http://caipiao.163.com/award/11xuan5/')
    content = response.html.find('section.main', first=True)
    body = content.find('tbody')
    itemDicts = dict()
    for tr in body:
        list = tr.find('td.start')
        for td in list:
            try:
                period = td.attrs['data-period']
                award = td.attrs['data-award']
                print("序號(hào):" + td.text + " 期號(hào):" + period + " 開(kāi)獎(jiǎng)號(hào)碼:" + award)
                itemDicts[period] = award
            except KeyError as e:
                print('except: ', e)
            finally:
                print('finally')
    sortItemDict = sorted(itemDicts.keys(), reverse=False)
    # print(sortItemDict)
    for key in sortItemDict:
        print("期號(hào):", key, " 開(kāi)獎(jiǎng)號(hào)碼:", itemDicts[key])


if __name__ == '__main__':
    getData()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容