Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Span tag data using Jsoup

I am trying to extract the specific content in html using Jsoup. Below is the sample html content.

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

Any help regarding the same?

like image 640
topblog Avatar asked Feb 25 '12 17:02

topblog


1 Answers

Does this not work for you? Not sure why you're arbitrarily starting at 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; it is very powerful.

like image 113
AHungerArtist Avatar answered Oct 22 '22 13:10

AHungerArtist