之前的requests庫(kù)號(hào)稱是給人用的請(qǐng)求庫(kù),而requests-html號(hào)稱是" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁(yè) > 營(yíng)銷資訊 > 網(wǎng)站運(yùn)營(yíng) > 大神kennethreitz寫(xiě)出requests-html,號(hào)稱為人設(shè)計(jì)的網(wǎng)頁(yè)解析庫(kù)

大神kennethreitz寫(xiě)出requests-html,號(hào)稱為人設(shè)計(jì)的網(wǎng)頁(yè)解析庫(kù)

時(shí)間:2023-09-04 22:18:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)

時(shí)間:2023-09-04 22:18:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)

大神kennethreitz寫(xiě)出requests-html,號(hào)稱為人設(shè)計(jì)的網(wǎng)頁(yè)解析庫(kù):requests庫(kù)的作者kennethreitz又設(shè)計(jì)出一個(gè)新的庫(kù)requests-html. 目前stars數(shù)高達(dá)9195




之前的requests庫(kù)號(hào)稱是給人用的請(qǐng)求庫(kù),而requests-html號(hào)稱是給人用的html解析庫(kù)。kennethreitz的牛掰我是相信的,他不會(huì)吹的。新庫(kù)的文檔我閱讀了一遍確實(shí)很不簡(jiǎn)單,以后學(xué)習(xí)爬蟲(chóng)可能再也不要requests+bs4作為起步的標(biāo)配了,直接用requests-html一個(gè)庫(kù)就可以搞定所有的事情。




我在谷歌趨勢(shì)搜索了requests-html,發(fā)現(xiàn)最早搜索是2018年1月。大鄧距離掌握爬蟲(chóng)圈最新技術(shù)落后了一年多,我知道的太晚了。以后大家有什么好的新的東西可以留言或者后臺(tái)留言。




requests-html強(qiáng)大之處在于:


  1. 擁有了requests之外的超強(qiáng)且神奇的頁(yè)面解析能力
  2. 完全支持javascript
  3. 定位元素支持CSS選擇器(jQuery,類似于pyquery庫(kù)的用法)、Xpath選擇器
  4. 訪問(wèn)過(guò)程偽裝成成瀏覽器行為模式(User-agent)
  5. 對(duì)于靜態(tài)頁(yè)面而言,本庫(kù)內(nèi)置自動(dòng)翻頁(yè),省去構(gòu)造網(wǎng)址的苦差事

安裝


文檔中說(shuō)目前支持python3.6,但是我經(jīng)過(guò)安裝和測(cè)試,在python3.7也能正常安裝和使用


pip install requests-html



智能翻頁(yè)(待改進(jìn))


這是我看到的最亮的功能,但是實(shí)際使用還是有問(wèn)題的,但是我仍要把ta列在第一個(gè)要講的內(nèi)容。平常我們寫(xiě)靜態(tài)網(wǎng)頁(yè)的爬蟲(chóng)前,需要先發(fā)現(xiàn)網(wǎng)址規(guī)律,如

第一頁(yè) https://book.douban.com/tag/小說(shuō)第二頁(yè) https://book.douban.com/tag/小說(shuō)?start=20&type=T第三頁(yè) https://book.douban.com/tag/小說(shuō)?start=40&type=T第四頁(yè) https://book.douban.com/tag/小說(shuō)?start=60&type=T

當(dāng)我們可能批量發(fā)起請(qǐng)求的時(shí)候,代碼需要這樣寫(xiě)

from bs4 import BeautifulSoupimport requests base = 'https://book.douban.com/tag/小說(shuō)?start={page}&type=Theaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}for i in range(100): url = base.format(page=i*20) resp = requests.get(url, headers=headers) bsObj = BeautifulSoup(resp.text, 'html.parser')

但是requests-html只需要

from requests_html import HTMLSessionsession = HTMLSession()r = session.get('https://book.douban.com/tag/小說(shuō)')for html in r.html: print(html)


<HTML url='https://book.douban.com/tag/%E5%B0%8F%E8%AF%B4'>

但是實(shí)際使用過(guò)程中該方法并未奏效,kennethreitz也在文檔中提到
There’s also intelligent pagination support (always improving)


always improving就是該庫(kù)在智能翻頁(yè)這塊表現(xiàn)還差強(qiáng)人意,還需要一直改進(jìn)。但是這個(gè)功能的設(shè)想真的很棒,期待早日更新出能使用的智能翻頁(yè)功能。
希望大家的心情不要希望->失望,其實(shí)后面還有很多精彩的內(nèi)容等待著你


正兒八經(jīng)的GET請(qǐng)求


我們對(duì)python編程語(yǔ)言的官網(wǎng) https://python.org/ ,發(fā)起GET請(qǐng)求的,并得到網(wǎng)站響應(yīng)Response對(duì)象。

該Response對(duì)象方法與requests庫(kù)的類似,我們看看常用的方法





from requests_html import HTMLSessionsession = HTMLSession()r = session.get('https://python.org/')r
Run and output!

<Response [200]>

獲取響應(yīng)的html文本數(shù)據(jù)

r.text[:50]
Run and output!

'<!doctype html>/n<!--[if lt IE 7]> <html class="n'

獲取響應(yīng)的html數(shù)據(jù),以二進(jìn)制顯示

r.content[:50]

Run and output!

b'<!doctype html>/n<!--[if lt IE 7]> <html class="n'

將響應(yīng)轉(zhuǎn)化為HTML類型,方便解析定位。

r.html

Run and output!

<HTML url='https://www.python.org/'>




HTML對(duì)象的方法








#混合著絕對(duì)和相對(duì)網(wǎng)址print(len(r.html.links))list(r.html.links)[:5]

Run and output!

119['/success-stories/category/arts/', 'https://kivy.org/', 'https://www.python.org/psf/codeofconduct/', 'http://www.scipy.org', 'https://docs.python.org/3/license.html']
htmlObj.absolute_links將相對(duì)路徑也轉(zhuǎn)化為絕對(duì)路徑

print(len(r.html.absolute_links))list(r.html.absolute_links)[:5]

Run and output!

119['https://kivy.org/', 'https://www.python.org/psf/codeofconduct/', 'http://www.scipy.org', 'https://jobs.python.org', 'https://docs.python.org/3/license.html']

Notes
相對(duì)路徑網(wǎng)址 //http://docs.python.org/3/tutorial/
絕對(duì)路徑網(wǎng)址 http://docs.python.org/3/tutorial/



我們發(fā)現(xiàn)兩種方法返回的網(wǎng)址數(shù)量都是119,所以absolute_links實(shí)際上對(duì)links中的相對(duì)路徑進(jìn)行了填充將其轉(zhuǎn)化為絕對(duì)路徑網(wǎng)址。


支持Javascript


requests-html支持javascrip,現(xiàn)在我們找一個(gè)網(wǎng)站 https://pythonclock.org/, 我們看到有一個(gè)倒計(jì)時(shí)時(shí)間表。這個(gè)頁(yè)面內(nèi)置了
javascript,像這種數(shù)據(jù)正常的網(wǎng)頁(yè)解析庫(kù)是無(wú)法解析到的。



from requests_html import HTMLSessionsession = HTMLSession()r2 = session.get('https://pythonclock.org/')r2.html.search('Python 2.7 will retire in...{}Enable Guido Mode')[0]

Run and output!

'</h1>/n </div>/n <div class="python-27-clock"></div>/n <div class="center">/n <div class="guido-button-block">/n <button class="js-guido-mode guido-button">'

requests-html有一個(gè)render渲染方法,可以用Chromium把javascript渲染出來(lái),但是第一次使用時(shí)會(huì)下載chromium,大概需要幾分鐘時(shí)間把。

r2.html.render()r2.html.search('Python 2 will retire in only {months} months!')

Run and output!

'</h1>/n </div>/n <div class="python-27-clock is-countdown"><span class="countdown-row countdown-show6"><span class="countdown-section"><span class="countdown-amount">1</span><span class="countdown-period">Year</span></span><span class="countdown-section"><span class="countdown-amount">2</span><span class="countdown-period">Months</span></span><span class="countdown-section"><span class="countdown-amount">28</span><span class="countdown-period">Days</span></span><span class="countdown-section"><span class="countdown-amount">16</span><span class="countdown-period">Hours</span></span><span class="countdown-section"><span class="countdown-amount">52</span><span class="countdown-period">Minutes</span></span><span class="countdown-section"><span class="countdown-amount">46</span><span class="countdown-period">Seconds</span></span></span></div>/n <div class="center">/n <div class="guido-button-block">/n <button class="js-guido-mode guido-button">'

上面的結(jié)果已經(jīng)得到了倒計(jì)時(shí)的數(shù)據(jù),接下來(lái)可以這樣提取時(shí)間

periods = [element.text for element in r.html.find('.countdown-period')]amounts = [element.text for element in r.html.find('.countdown-amount')]countdown_data = dict(zip(periods, amounts))countdown_data

Run and output!

{'Year': '1', 'Months': '2', 'Days': '5', 'Hours': '23', 'Minutes': '34', 'Seconds': '37'}



CSS定位


從HTML對(duì)象中抽取指定位置的元素
htmlObj.find('元素選擇器', first=False) 返回滿足條件的所有Element元素,返回的數(shù)據(jù)類型是由Element組成的列表。

r.html.find('#about')

Run and output!

[<Element 'li' aria-haspopup='true' class=('tier-1', 'element-1') id='about'>]

將first設(shè)置為True,只返回滿足條件的第一個(gè)Element,此時(shí)返回的不是列表,而是Element。


about = r.html.find('#about',first=True)about

Run and output!

<Element 'li' aria-haspopup='true' class=('tier-1', 'element-1') id='about'>



Element對(duì)象方法








r = session.get('https://github.com/')htmlObj = r.htmlhtmlObj.xpath('a',first=True)

Run and output!

<Element 'a' class=('btn', 'ml-2') href='https://help.github.com/articles/supported-browsers'>

更多內(nèi)容請(qǐng)看requests-html文檔 http://html.python-requests.org/




往期文章


Python系列課(爬蟲(chóng)、文本分析、機(jī)器學(xué)習(xí))視頻教程
【薦】3月初python圈中的精選文章

少有人知的python數(shù)據(jù)科學(xué)庫(kù)

職場(chǎng)達(dá)人必備技能:群發(fā)營(yíng)銷內(nèi)容
【動(dòng)畫(huà)】如何用scrapy命令行訪問(wèn)、解析網(wǎng)頁(yè)數(shù)據(jù)
100G 文本分析語(yǔ)料資源(免費(fèi)下載)
十分鐘帶你入門最python風(fēng)格的Gui庫(kù)
字符串格式化你不得不知的那些事兒
手把手教你學(xué)會(huì)LDA話題模型可視化pyLDAvis庫(kù)
【工具篇】如何用Google Colab高效的學(xué)習(xí)Python
使用Python制作WORD報(bào)告
使用Pandas、Jinja和WeasyPrint制作pdf報(bào)告





關(guān)鍵詞:設(shè)計(jì),號(hào)稱

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉