Geek
Geek

Reputation: 3329

JSoup select Div based on Id and href based on title

Im using JSoup to parse HTML response. I have multiple Div tags. I have to select Div tag based on an ID.

My pseudo code looks like this,

Document divTag = Jsoup.connect(link).get();
Elements info = divTag.select("div#navDiv");

where navDiv is the ID. But it doesnt seem to work.

Also I would want to select Href inside the Div based on some title, where hrefTitle[] would be string array. So while iterating the href I would check if the title is present in the string array, if so i would add them to list else ignore. How do i select href inside Div ? and How to select title? any inputs much appreciated.

Upvotes: 2

Views: 4084

Answers (1)

Wayne
Wayne

Reputation: 60424

But it doesnt seem to work.

It should work. Proof:

Document doc = Jsoup.parse("<html><body><div/>" + 
    "<div id=\"navDiv\">" + 
        "<a href=\"href1\">link1</a>" +
        "<a href=\"href2\">link2</a><" +
    "</div></body></html>");
Element div = doc.select("div#navDiv").first();

Now, we can select the a element inside the div that has (for example) an href attribute whose value is href2:

System.out.println(div.select("a[href=href2]"));

Output:

<a href="href2">link2</a>

You can find the full selector syntax here:

Upvotes: 1

Related Questions