Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse HTML with CSS or XPath selectors?

My goal is to parse HTML with lxml, which supports both XPath and CSS selectors.

I can tie my model properties either to CSS or XPath, but I'm not sure which one would be the best, e.g. less fuss when HTML layout is changed, simpler expressions, greater extraction speed.

What would you choose in such a situation?

like image 775
ovolko Avatar asked Jan 22 '23 06:01

ovolko


1 Answers

Which are you more comfortable with? Most people tend to find CSS selectors easier and if others will maintain your work, you should take this into account. One reason for this might be that there's less worrying about XML namespaces which are the source of many a bug. CSS selectors tend to be more compact than the equivalent XPath, but only you can decide whether that's relevant factor or not. I would note that it's not an accident that jquery's selection language is modelled on CSS selectors and not on XPath.

On the other hand, XPath is a more expressive language for general DOM manipulation. For example, there's no CSS selector equivalent of the "parent" or "ancestor" axes, nor is there a way to directly address text nodes equivalent to "text()" in XPath. In contrast, I can't think of any DOM path that can be expressed in CSS selectors but not in XPath, although E[foo~="warning"] and E[lang|="en"] are distinctly tricky in XPath.

What CSS selectors do have that XPath doesn't are pseudo-classes, though if you're doing server side DOM manipulation, these are not likely to be of use to you.

As for which results in greater extraction speed, I don't know lxml, but I would expect equivalent paths to have very similar performance characteristics.

like image 102
Alohci Avatar answered Jan 28 '23 04:01

Alohci