在Parse HTML的時候,許多人都是由先由Beautifulsoup著手,因為我也是XD
不過,前不久接觸到另一個解析HTML的工具,叫做request_html,也是由requests函式庫的開發者所開發。
它的主旨是:"HTML Parsing for Humans!",就是希望能讓使用者能更簡單、直覺地操作。
而在request_html library的官方Github上,有特別介紹當你使用request_html之優點:
以下:
1. Full JavaScript support!
2. CSS Selectors (a.k.a jQuery-style, thanks to PyQuery).
3. XPath Selectors, the faint at heart.
4. Mocked user-agent (like a real web browser).
5. Automatic following of redirects.
6. Connection–pooling and cookie persistence.
7. The Requests experience you know and love, with magical parsing abilities.
8. Async Support特別是第一點與第八點,是作者特別強調的重點,在後面會有一點點介紹。
同樣地以ptt-gossip版為例子。
假設,我們今天想爬下一個頁面中的所有文章標題,以request_html實作的話,該如何著手?
Review: 由前篇之get_web_page函數,我們回傳了resp.text。
from requests_html import HTML
url = 'https://www.ptt.cc/bbs/Gossiping/index.html'
resp_text = get_web_page(url)
def get_web_title(doc):
html = HTML(html=doc)
titles = html.find('div.r-ent')
return titles
titles = get_web_title(resp_text)
print(titles)