Search code examples
pythonpyquery

How do I access the first item(or xth item) in a PyQuery query?


I have a query for a one of my tests that returns 2 results. Specifically the 3rd level of an outline found using

query = html("ul ol ul")

How do I select the first or second unordered list?

query[0]

decays to a HTMLElement

list(query.items())[0]

or

query.items().next() #(in case of the first element)

is there any better way that I can't see?

note:

query = html("ul ol ul :first")

gets the first element of each list not the first list.


Solution

  • From the PyQuery documentation on traversing, you should be able to select the first unordered list by using:

    query('ul').eq(0)
    

    Thus the second unordered list can be obtained by using:

    query('ul').eq(1)