使用Jsoup提取Span标记数据

top*_*log 5 java web-crawler jsoup

我试图使用Jsoup在html中提取特定内容.以下是示例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>
Run Code Online (Sandbox Code Playgroud)

我使用以下命令获得了所需的输出:

document.select(".price-table").select(".line").select("span").get(2).text()
Run Code Online (Sandbox Code Playgroud)

看起来很冗长.我不能直接使用span类("价格最终价格我们的fksk-our")?

对此有何帮助?

AHu*_*ist 5

这不适合你吗?不知道为什么你是任意开始price-table.

doc.select("span[class=price final-price our fksk-our]").text();
Run Code Online (Sandbox Code Playgroud)

如果没有,它应该非常接近.看看JSoup的选择器语法 ; 它非常强大.