这是
HTML的一部分(问题简化):
<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>
我想得到的是包含拍卖ID的矢量,其中显示2.png图像(在这种情况下id = 4672).如何构造Selector查询以获取此信息?
http://jsoup.org/apidocs/org/jsoup/select/Selector.html – 在这里我只能找到如何选择孩子,而不是父母……
任何帮助,包括其他库的使用.我尝试过Jsoup,因为它似乎是最受欢迎的.
解决方法
您可以使用parent()方法:
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. // ... }