4

Is there a Python library that can parse an CSS selector and emit an equivalent XPath expression? For Ruby, there is Nokogiri.

2 Answers 2

10

I recently took over cssselect and made it an independent project after extracting it from lxml: http://packages.python.org/cssselect/

It does precisely what the question asks for. The XPath expressions produced can then be used in lxml. PyQuery actually uses cssselect (formerly lxml.cssselect)

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, nice, thanks for the follow-up. So lxml.cssselect.css_to_xpath is obsolete? Was it ever an official feature? Nice work indeed.
@NiklasB. it was an implementation detail and will go away with lxml 2.4 (not released yet) when lxml.cssselect becomes a thin wrapper around cssselect. The CSSSelector class and HtmlElement.cssselect method remain for compatibility
I really wish lxml updated their docs often enough to mention that cssselect was taken out despite previously being packaged. but thanks
@Amalgovinus, file a bug?
4

How about pyQuery?

Answer based on PHP equivalent of PyQuery or Nokogiri?

1 Comment

I don't see how this answers my question, and the documentation is not the best I've seen. Can you point me to the functionality that I am looking for? (Converting CSS to XPath, that is). EDIT found it! It's actually part of lxml though, but thanks for the hint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.