简体   繁体   English

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

[英]Jsoup how to check if an element has a specific child element

How to check if a div element has a span element as a child or not. 如何检查div元素是否具有span元素作为子元素。

I have a html page has a div of col-md-6 class name, the div itself sometimes has a span of price class and sometimes hasn't. 我有一个html页面,其div具有col-md-6类的名称,该div本身有时具有跨度的price类,有时却没有。

 <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> 
I tried this code but It didn't print 0s as expected ! 我尝试了这段代码,但是没有按预期输出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(); } 

The selector you need is div.col-md-6 span[class=price] - find a div with class col-md-6 and then look for a descendant which is a span named price - notice the space between the two parts of the query! 你需要选择是div.col-md-6 span[class=price] -找到一个div带班col-md-6然后找一个传人是一个span命名为price -注意的两个部分之间的空间查询!
Example code - 示例代码-

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");

And the output - 和输出-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM