简体   繁体   中英

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. Any other class name without - works fine.

That's a bug in the parser in lxml.cssselect. I took over maintenance of the project and extracted it from lxml. The bug is fixed in the new cssselect: http://packages.python.org/cssselect/

lxml 2.4 will use the new cssselect, but until then the way to use it is:

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 ')]")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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