简体   繁体   中英

Using jsoup for extracting price

I wanna get the price 9.99 from this page source. https://www.walmart.com/ip/Terminator-Genisys-DVD/45863333?sourceid=api00ctd43f4bc7559f459fae574f62a0e9de01&affp1=%7Capk%7C&affilsrc=api&veh=aff&wmlspartner=readonlyapi

The code I am using is

    public String doubleCheckPrice(String html, IDoubleCheckable availability) throws URISyntaxException, IOException{
    Document doc = Jsoup.parse(html);
    String price = null;

    for(Element meta : doc.select("div")) {
      if((meta.attr("itemprop") != null) && (meta.attr("itemprop").equals("price"))) {
        price = meta.text();
        price = price.replace("$", "").trim();
        logger.debug("Extracted price via double check {} for availability {}", price, availability.getUrl());
      }
    }

    if(price == null) {
      Elements elements = doc.select(".js-price-display");
      if(elements != null && elements.size() > 0) {
        price = elements.get(0).text();
        price = price.replace("$", "").trim();
      }
    }

    return price;
  }

But I am getting null. Any help will be appreciated. Thanks

I think you should use Walmart's API for this purpose. That is the best way.

Alternatively, if you cannot use an API, you should use a framework for this. Have a look at it https://jsoup.org/

This framework will allow you to create a structured document and help you to iterate tags, classes or IDs. You can then use findElementsById to fetch the data. Have a look at the examples of the site.

I got the solution for this.Here it is

for(Element meta : doc.select(".Price-group")) {

        if(meta.attr("aria-label")!=null)
        {
            System.out.println(meta.attr("aria-label"));
            price=meta.text();
            price = price.replace("$", "").trim();
            logger.debug("Extracted price via double check {} for availability {}", price, availability.getUrl());


        }

Here is the solution

Elements priceElms=document.select(".prod-BotRow.prod-showBottomBorder.prod-OfferSection .prod-PriceHero .Price-group");
if(priceElms.size() > 0){
String price=priceElms.get(0).text();
price=price.replace("$","");
}

No need to loop to get the values, Just select the appropriate field you want and use Jsoup selectors. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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