lxml 使用 cssselector 检索奇怪的项目

发布于 2024-07-28 23:13:48 字数 377 浏览 3 评论 0原文

在我的测试文档中,我有几个标记为“item”的类,目前我正在使用以下内容来解析带有此类的html文件中的所有内容,

Selection = html.cssselect(".item")

我希望它选择所有奇怪的项目,就像在使用JQuery的javascript中一样

Selection = $(".item:odd");

逐字尝试我得到以下错误

lxml.cssselect.ExpressionError: The psuedo-class Symbol(u'odd', 6) isknown

我知道这对我自己实现来说是微不足道的,我想知道 lxml 是否支持原生地。

In my test document I have a few classes labeled "item", currently I'm using the following to parse everything in the html file with this class with

Selection = html.cssselect(".item")

I'd like it to select all the odd items, like this in javascript using JQuery

Selection = $(".item:odd");

Trying that verbatim I get the following error

lxml.cssselect.ExpressionError: The psuedo-class Symbol(u'odd', 6) is unknown

I know this would be trivial to implement on my own, I was wondering if this is supported by lxml natively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余厌 2024-08-04 23:13:48

“奇数”和“偶数”功能是名为“nth-child()”的选择器的一部分; 查看 CSS 选择器规范以获取更多详细信息:

http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#nth-child-pseudo

因此,您应该能够通过以下方式准确获得您想要的行为(并且它对我来说适用于 CSSSelector):

".item:nth-child(odd)"

The "odd" and "even" features are part of a selector named "nth-child()"; take a look at the CSS selector specification for more details:

http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#nth-child-pseudo

Therefore, you should be able to get exactly the behavior you want (and it works for me with CSSSelector here) with:

".item:nth-child(odd)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文