lxml 使用 cssselector 检索奇怪的项目
在我的测试文档中,我有几个标记为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“奇数”和“偶数”功能是名为“nth-child()”的选择器的一部分; 查看 CSS 选择器规范以获取更多详细信息:
因此,您应该能够通过以下方式准确获得您想要的行为(并且它对我来说适用于 CSSSelector):
The "odd" and "even" features are part of a selector named "nth-child()"; take a look at the CSS selector specification for more details:
Therefore, you should be able to get exactly the behavior you want (and it works for me with CSSSelector here) with: