問題描述
是否有任何 Python 庫可以讓我解析類似于 jQuery
的 HTML 文檔?
Is there any Python library that allows me to parse an HTML document similar to what jQuery
does?
即我希望能夠使用 CSS 選擇器語法 從文檔中抓取任意一組節點,讀取它們的內容/屬性等.
i.e. I'd like to be able to use CSS selectors syntax to grab an arbitrary set of nodes from the document, read their content/attributes, etc.
我以前使用過的唯一 Python HTML 解析庫是 BeautifulSoup,盡管它很好,但我一直認為如果我有可用的 jQuery 語法,我的解析會更快.:D
The only Python HTML parsing lib I've used before was BeautifulSoup, and even though it's fine I keep thinking it would be faster to do my parsing if I had jQuery syntax available. :D
推薦答案
如果你精通BeautifulSoup,您只需將 soupselect 添加到您的庫中.
Soupselect 是 BeautifulSoup 的 CSS 選擇器擴展.
If you are fluent with BeautifulSoup, you could just add soupselect to your libs.
Soupselect is a CSS selector extension for BeautifulSoup.
用法:
from bs4 import BeautifulSoup as Soup
from soupselect import select
import urllib
soup = Soup(urllib.urlopen('http://slashdot.org/'))
select(soup, 'div.title h3')
[<h3><span><a href='//science.slashdot.org/'>Science</a>:</span></h3>,
<h3><a href='//slashdot.org/articles/07/02/28/0120220.shtml'>Star Trek</h3>,
..]
這篇關于在 Python 中進行類似 jquery 的 HTML 解析?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!