Jsoup: how to select parent nodes that have children matching the condition

Here's the HTML part (simplified for the question):

<a href="/auctions?id=4672" class="auction sec"> <div class="progress"> <div class="guarantee"> <img src="/img/ico/2.png" /> </div> </div> </a> <a href="/auctions?id=4670" class="auction"> <div class="progress"> <div class="guarantee"> <img src="/img/ico/1.png" /> </div> </div> </a> 

What I want to get is a vector containing auction IDs for which 2.png image is displayed (id = 4672 in this case). How to create a Selector request to get this?

http://jsoup.org/apidocs/org/jsoup/select/Selector.html - Here I can only find how to choose children, not parents ...

Any help is appreciated, including using other libraries. I tried Jsoup because it seemed the most popular.

+8
html parsing parent jsoup children
source share
1 answer

You can use the parent() method:

 final String html = "<a href=\"/auctions?id=4672\" class=\"auction sec\"> \n" + " <div class=\"progress\"> \n" + " <div class=\"guarantee\"> \n" + " <img src=\"/img/ico/2.png\" /> \n" + " </div> \n" + " </div> </a>\n" + "<a href=\"/auctions?id=4670\" class=\"auction\"> \n" + " <div class=\"progress\"> \n" + " <div class=\"guarantee\"> \n" + " <img src=\"/img/ico/1.png\" /> \n" + " </div> \n" + " </div> </a>"; Document doc = Jsoup.parse(html); for( Element element : doc.select("img") ) // Select all 'img' tags { Element divGuarantee = element.parent(); // Get parent element of 'img' Element divProgress = divGuarantee.parent(); // Get parent of parent etc. // ... } 
+10
source share

All Articles