JQuery/CSS 个选择器在 Python 中?

JQuery/CSS selectors in Python?

我用 Python 解析 HTML。

解析后我在树中搜索一些元素。

到目前为止,我还没有找到在树中查找元素的好用方法。 XPath 可用,但我更喜欢熟悉的方式。

有没有办法在 Python 中使用语法类似于 jquery/css 选择器的选择器?

BeautifulSoup 具有 CSS 选择器支持 内置 :

>>> from bs4 import BeautifulSoup
>>> from urllib2 import urlopen
>>> soup = BeautifulSoup(urlopen("https://google.com"))
>>> soup.select("input[name=q]")
[<input autocomplete="off" class="lst" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top" title="Google Search" value=""/>]

还有cssselect package that you can use in combination with lxml.

请注意,某些 limitations in how CSS selectors work in BeautifulSoup - lxml+csselect 支持更多 CSS 选择器:

This is all a convenience for users who know the CSS selector syntax. You can do all this stuff with the Beautiful Soup API. And if CSS selectors are all you need, you might as well use lxml directly: it’s a lot faster, and it supports more CSS selectors. But this lets you combine simple CSS selectors with the Beautiful Soup API.

有一个名为 pyquery 的库:https://pypi.python.org/pypi/pyquery

这是文档中的示例:

>>> d = pq("<option value='1'><option value='2'>")
>>> d('option[value="1"]')
[<option>]