Html jsoup:如何选择父节点,其中子节点满足一个条件

Html jsoup:如何选择父节点,其中子节点满足一个条件,html,parsing,jsoup,parent,children,Html,Parsing,Jsoup,Parent,Children,以下是HTML的一部分(问题简化): 我想要得到的是包含拍卖id的向量,为其显示2.png图像(本例中id=4672)。如何构造选择器查询以获得此结果 -在这里,我只能找到如何选择孩子,而不是父母 感谢您的帮助,包括使用其他库。我尝试过Jsoup,因为它似乎是最流行的。您可以使用parent()方法: final String html = "<a href=\"/auctions?id=4672\" class=\"auction sec\"> \n" + "

以下是HTML的一部分(问题简化):


我想要得到的是包含拍卖id的向量,为其显示2.png图像(本例中id=4672)。如何构造选择器查询以获得此结果

-在这里,我只能找到如何选择孩子,而不是父母

感谢您的帮助,包括使用其他库。我尝试过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.

    // ...
}
final String html=“\n”
+ "";
Document doc=Jsoup.parse(html);
for(元素:doc.select(“img”)//选择所有“img”标记
{
Element divGuarrance=Element.parent();//获取'img'的父元素
Element divProgress=DivGuarrance.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.

    // ...
}