2012-07-27 62 views
3

我使用jsoup從休耕網頁 { http://www.jcpenney.com/dotcom/jewelry-watches/fine-jewelry/mens-jewelry/bulova%25c2%25ae-mens-stainless-steel-watch/prod.jump?ppId=180d97e&catId=cat100240089&selectedLotId=0514592&selectedSKUId=05145920000&navState=navState-:catId-cat100240089:subcatId-:subcatZone-false:N-100240089%20158:Ns-:Nao-0:ps-24:pn-1:Ntt-:Nf-:action-guided%20navigation&catId=SearchResults } retrive圖像 我的代碼是jsoup圖像是沒有得到解析

String url = "http://www.jcpenney.com/dotcom/jewelry-watches/fine-jewelry/mens-jewelry/bulova%25c2%25ae-mens-stainless-steel-watch/prod.jump?ppId=180d97e&catId=cat100240089&selectedLotId=0514592&selectedSKUId=05145920000&navState=navState-:catId-cat100240089:subcatId-:subcatZone-false:N-100240089%20158:Ns-:Nao-0:ps-24:pn-1:Ntt-:Nf-:action-guided%20navigation&catId=SearchResults"; 


      Document doc= Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2").get(); 


      String imgUrl=doc.select("#mapImageSjElement4 img").attr("abs:src"); 

它應該返回我的圖像的URL,但我沒有得到圖片的網址。有什麼建議麼????? 我想檢索網頁左側的主要產品圖片。

回答

0

如果您打印整個文檔,您會看到該網站中的圖像以及更多內容都是通過散佈在整個頁面上的JavaScript腳本加載的。爲了獲得該圖像,您必須在2:

之間選擇:
  1. 使用像Selenium,W​​ebdriver,HTTPClient這樣的無GUI網頁瀏覽器;並在頁面的滿負荷,得到它的HTML內容
  2. 通過研究它的代碼模仿JavaScript和檢索數據,你想

這將是使用第二approache我不添加任何提到的方式額外的庫到您的項目:

//Let's say you have the right script in a String 
//variable named javascript. 
String[] html = javascript.split("\n"); 

String imgUrl = ""; 
for(String line : html) { 
    if (line.contains("imgUrl variable name here")) { 
     imgUrl = line; 
     break; 
    } 
} 

//Now that you have what you want in a variable 
//just split/substring it, untill you narrowed 
//it down to what you want. 
+0

我正在打印其實我檢索到的圖像是潛水標籤內,該分區是不是在jsoup回到我的任何想法如何以檢索缺少內容的HTML獲取的HTML。我做了不同的事情,比如添加cookies,也使用了useragents,但都是徒勞的。 – 2012-07-31 05:00:02

+0

正如我看着它,它是由JavaScript加載。使用Josup,你必須進入所有的腳本,看看它是如何加載它。如果有任何機會,它是從數據庫直接獲取數據的Jquery,Jsoup只是不會這樣做 – 2012-07-31 11:10:58

+0

我認爲圖像src存儲在一個JavaScript變量中,我可以獲取腳本標記,但我可以獲取存儲在變量中的值裏面的腳本使用jsoup ??????如果是的話那怎麼樣? – 2012-07-31 15:39:55