Jos Sat
Jos Sat

Reputation: 83

Get href those who have title using jsoup parsing

When I parsed the URL I got several hrefbut I want the hrefwhich has title.Kindly let me get the answer

<a href="detailnews.asp?newsid=18318" title="Power shutdown areas in Chennai on 13-04-15">
<a href="detailnews.asp?newsid=18318">

My jsoup CSS query be like this to get the href

#table13>tbody>tr>td>a

Upvotes: 1

Views: 245

Answers (1)

Jonas Czech
Jonas Czech

Reputation: 12348

Change your CSS selector like this: #table13>tbody>tr>td>a[title]. The "title" in brackets will get you only a which have a "title" attribute. Some code:

Document myDocument = Jsoup.connect("http://example.com/").get();

//selects "a" with a "title" attribute
Elements elements = myDocument.select("#table13>tbody>tr>td>a[title]");

for (Element e : elements) {
   System.out.println(e.attr("title")); //print contents of "title" attribute

   System.out.println(e.attr("href")); //print contents of "href" attribute
}

Upvotes: 2

Related Questions