views:

52

answers:

1

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?

+2  A: 

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.

Alohci