简体   繁体   English

用破折号('-')搜索类名

[英]Searching on class names with a dash ('-')

I'm messing around with lxml in Python, but can't seem to figure out how to use the cssselect() function to get all div 's with the class reddit-entry , as it seems to dislike the - character. 我在搞乱Python中的lxml ,但似乎无法弄清楚如何使用cssselect()函数来获取所有带有reddit-entry类的div ,因为它似乎不喜欢-字符。 Any other class name without - works fine. 任何其他没有-类名都可以正常工作。

That's a bug in the parser in lxml.cssselect. 这是lxml.cssselect中的解析器中的错误。 I took over maintenance of the project and extracted it from lxml. 我接管了该项目的维护工作,并从lxml中提取了该项目。 The bug is fixed in the new cssselect: http://packages.python.org/cssselect/ 该错误已在新的cssselect中修复: http ://packages.python.org/cssselect/

lxml 2.4 will use the new cssselect, but until then the way to use it is: lxml 2.4将使用新的cssselect,但在此之前,使用它的方法是:

from cssselect import HTMLTranslator
result = lxml_document.xpath(HTMLTranslator().css_to_xpath('div.reddit-entry'))

如果您通过xpath运行cssselector使用的代码,它确实可以工作...

obj.xpath("//div[contains(concat(' ', normalize-space(@class), ' '), ' reddit-entry ')]")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM