Question

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.

Était-ce utile?

La solution

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

Autres conseils

If you run the code that cssselector uses via xpath it does work...

obj.xpath("//div[contains(concat(' ', normalize-space(@class), ' '), ' reddit-entry ')]")
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top