简体   繁体   中英

Parsing HTML with Jsoup

I'm trying to do some parsing and I'm stuck... Here's the structure of HTML:

<ul class="sub-menu"> 
<li id="1" class="1"><a href="http://link">SOME TEXT</a> 
    <ul class="sub-menu"> 
        <li .... ><a ... /></li>
        <li .... ><a ... /></li>
        <li .... ><a ... /></li>
    </ul>
</li>
<li id="2" class="2"><a href="http://link2">SOME OTHER TEXT</a> 
    <ul class="sub-menu"> 
        <li .... ><a ... /></li>
        <li .... ><a ... /></li>
        <li .... ><a ... /></li>
    </ul>
</li></ul>

I need to get each li (id = 1, 2 and s) and then lis inside them ( <li .... ><a ... /></li> ).

Here's how my Java looks:

// ul contains the source above
Elements lis = ul.select("li"); // I know that this line screws up everything here, but I can't figure out how to do it correctly
for(Element li: lis)
{
    String text = li.select("a").first().text();
    Elements lis2 = li.select("ul[class=sub-menu]").first().getElementsByTag("li");     
    for(Element li2: lis2)
    {
        Element a = li2.select("a").first();
        // and other stuff with 'a'
    }
}

So can anybody help me to solve this problem?

EDIT: The problem is that ul.select("li"); returns every single 'li' in source I wrote here. I need to get lis with id 1, 2 and so on. And then I need to get those <li .... ><a ... /></li> . PS Sorry for my bad English.

I'm not sure, but try something like this

for( Element element : doc.select("[li]") )
{
    if( element.attr("id")== 1 || element.attr("id").getValue()== 2 )
    {
        // thats your elements 'element'
        System.out.println(element);
    }
}

Regards, Hugo Pedrosa

Have you tried

`ul.children()`

I think that it will return only the immediate children nodes of ul .

Use the comparing methods built in JSoup, such as <, > etc.

You can select elements by including a pseudo selector that will look at the relative position in the DOM structure relative to it's parent:

Elements lis = ul.select("li:lt(2)");

which should result in only returning the li's 0 and 1.

Please refer to the JSoup documentation for pseudo selectors which explains this better than I can!

http://jsoup.org/cookbook/extracting-data/selector-syntax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM