2017-07-17 156 views
1

我想從此頁面源獲得價格9.99。 https://www.walmart.com/ip/Terminator-Genisys-DVD/45863333?sourceid=api00ctd43f4bc7559f459fae574f62a0e9de01&affp1=%7Capk%7C&affilsrc=api&veh=aff&wmlspartner=readonlyapi使用jsoup提取價格

我使用的代碼是

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; 
    } 

但是我得到空。任何幫助將不勝感激。 謝謝

+0

@ saka1029準確地在你想要我嘗試的代碼中。我對此感到陌生,你能告訴我嗎? – user3128634

回答

0

我認爲你應該使用沃爾瑪的API來達到這個目的。這是最好的方法。

另外,如果你不能使用API​​,你應該使用這個框架。看看它https://jsoup.org/

這個框架將允許你創建一個結構化的文檔,並幫助你迭代標籤,類或ID。然後您可以使用findElementsById來獲取數據。看看這個網站的例子。

0

我得到this.Here的解決方案是

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()); 


     } 
0

下面是解

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("$",""); 
} 

無需循環得到的值,只要選擇你要適當的領域和用途Jsoup選擇器。 謝謝