【问题标题】:Extract Span tag data using Jsoup使用 Jsoup 提取 Span 标签数据
【发布时间】:2012-02-25 17:31:20
【问题描述】:

我正在尝试使用 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>

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

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

看起来很长。 我不能直接使用 span 类(“price final-price our fksk-our”)吗?

对此有任何帮助吗?

【问题讨论】:

    标签: java web-crawler jsoup


    【解决方案1】:

    这不适合你吗?不知道你为什么随意从price-table开始。

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

    如果不是,它应该非常接近。看JSoup的selector syntax;它非常强大。

    【讨论】:

    • 非常感谢AHungerArtist,您提供的解决方案运行良好。我发现“price-table” div 类在整个 html 中是独一无二的,所以我选择了那个类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多