views:

133

answers:

1

In my test document I have a few classes labeled "item", currently I'm using the following to parse everything in the html file with this class with

Selection = html.cssselect(".item")

I'd like it to select all the odd items, like this in javascript using JQuery

Selection = $(".item:odd");

Trying that verbatim I get the following error

lxml.cssselect.ExpressionError: The psuedo-class Symbol(u'odd', 6) is unknown

I know this would be trivial to implement on my own, I was wondering if this is supported by lxml natively.

+1  A: 

The "odd" and "even" features are part of a selector named "nth-child()"; take a look at the CSS selector specification for more details:

http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#nth-child-pseudo

Therefore, you should be able to get exactly the behavior you want (and it works for me with CSSSelector here) with:

".item:nth-child(odd)"
Brandon Craig Rhodes