简体   繁体   English

使用Jsoup提取Span标签数据

[英]Extract Span tag data using Jsoup

I am trying to extract the specific content in html using Jsoup. 我正在尝试使用Jsoup提取html中的特定内容。 Below is the sample html content. 以下是示例html内容。

<html xmlns="http://www.w3.org/1999/xhtml">
 <head> 
 </head>
 <body class="">
  <div class="shop-section line bmargin10 tmargin10">
   <div class="price-section fksk-price-section unit">
    <div class="price-table">
     <div class="line" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
      <div class="price-save">
       <span class="label-td"><span class="label fksk-label">Price :</span></span>
      </div>
      <span class="price final-price our fksk-our" id="fk-mprod-our-id">Rs.<span class="small-font"> </span>11990</span>
     </div>
     <meta itemprop="price" content="Rs. 11990" />
     <meta itemprop="priceCurrency" content="INR" />
     <div class="our-price-desc fksk-our-price-desc">
      <small>(Prices are inclusive of all taxes)</small>
     </div>
    </div>
   </div>
  </div>
 </body>
</html>

I got the required output using below command: 我使用以下命令获得了所需的输出:

document.select(".price-table").select(".line").select("span").get(2).text()

Looks like its lengthy. 看起来很长。 Can't i able to directly get using span class ("price final-price our fksk-our")? 我不能直接使用span类(“为我们的fksk-our定价最终价格”)吗?

Any help regarding the same? 有什么帮助吗?

Does this not work for you? 这对您不起作用吗? Not sure why you're arbitrarily starting at price-table . 不知道为什么您要从price-table

doc.select("span[class=price final-price our fksk-our]").text();

If not, it should be pretty close. 如果没有,它应该非常接近。 Look at JSoup's selector syntax ; 看一下JSoup的选择器语法 it is very powerful. 它非常强大。

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

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