Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lxml/Python : get previous-sibling

Tags:

python

lxml

I have the following html:

<div id = "big">
    <span>header 1</span>
    <ul id = "outer">
        <li id = "inner">aaa</li>
        <li id = "inner">bbb</li>
    </ul>

    <span>header 2</span>
    <ul id = "outer">
        <li id = "inner">ccc</li>
        <li id = "inner">ddd</li>
    </ul>
</div>

I want it to loop it in the order:

header 1
aaa
bbb
header 2
ccc
ddd

I have tried looping through each ul and then printing the header and the li values. However, I don't know how to get the span header that is associated with a ul.

sets = tree.xpath("//div[@id='big']//ul[@id='outer']")

for set in sets:

    # Print header. Not sure how to get it
    header = set.xpath(".//li/preceding-sibling::span")
    print header 

    # Print texts. This works.
    values = set.xpath(".//li//text()")
    for v in values:
        print v 

Just looping all text nodes won't work because I need to know if it is a header or li value.

like image 218
user984003 Avatar asked Apr 28 '13 11:04

user984003


1 Answers

This worked:

header = ingred_set.getprevious().xpath(".//text()")[0]
like image 182
user984003 Avatar answered Oct 17 '22 01:10

user984003