Jsoup如何检查元素是否具有特定的子元素

时间:2018-05-26 18:52:25

标签: java jsoup

如何检查div元素是否具有span元素作为子元素。

我有一个html页面有一个div-col-md-6类名,div本身有时会有一个价格等级的范围,有时却没有。

<div class="col-md-6">
<ul> <li> 
<span class="price">
<strong class="bold">70,000 </strong>USD</span> 
</li>
<li> 
</li>
<li>8775 views </li> 
</ul>
</div>

    
<div class="col-md-6">
<ul>
<li>
</li>
<li>yesterday</li>
<li>53 views</li>
</ul>
</div>
我尝试了这段代码,但没有按预期打印0!

 try 
 {
 Document doc = Jsoup.connect("https://www.bezaat.com/ksa/riyadh/سيارات/all").userAgent("Mozilla/5.0(Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0").referrer("http://www.google.com").ignoreHttpErrors(true).get();  

 Elements EE = doc.select("div.col-md-12").select("div.col-md-6") ; 

 Elements E = doc.select("div.col-md-12").select("div.media.ads-style.reg").select("div.media-left"); 

 for(int i = 0;i < E.size();i++)
 {

 if(EE.get(i).select("span.price").text() !=  null)
 { 
 System.out.println(EE.get(i).select("span.price").text().replaceAll("[^0-9]",""));  
 }
 else
 {
  System.out.println("0"); 
 }}
 }
 catch(java.io.IOException e)
 {
  e.printStackTrace();
  }

1 个答案:

答案 0 :(得分:1)

您需要的选择器是div.col-md-6 span[class=price] - 找到一个div的班级col-md-6,然后找一个名为span price的后代 - 通知查询的两个部分之间的空间!
示例代码 -

String html1 = "<div class=\"col-md-6\">" +
            "<ul> <li> " +
            "<span class=\"price\">" +
            "<strong class=\"bold\">70,000 </strong>USD</span> " +
            "</li>" +
            "<li> " +
            "</li>" +
            "<li>8775 views </li> " +
            "</ul>" +
            "</div>";
String html2 = "<div class=\"col-md-6\">" +
            "<ul>" +
            "<li>" +
            "</li>" +
            "<li>yesterday</li>" +
            "<li>53 views</li>" +
            "</ul>" +
            "</div>";
Document doc1 = Jsoup.parse(html1);
Elements price1 = doc1.select("div.col-md-6 span[class=price]");
if (price1.size() > 0)
    System.out.println(price1.html());
else
    System.out.println("not found in html1");
Document doc2 = Jsoup.parse(html2);
Elements price2 = doc2.select("div.col-md-6 span[class=price]");
if (price2.size() > 0)
    System.out.println(price2.html());
else
    System.out.println("not found in html2");

输出 -

<strong class="bold">70,000 </strong>USD not found in html2