2011-06-16 83 views
8

我試圖抓取出價網站的內容,但無法獲取網站的完整頁面。我在xulrunner上使用crowbar首先獲取頁面(因爲ajax以懶惰的方式加載某些元素),然後從文件中刪除。 但是在bidrivals網站的主頁上,即使本地文件格式正確,也會失敗。 jSoup只是在html代碼中間似乎以'...'字符結尾。 如果有人遇到過這個,請幫忙。 以下代碼被稱爲[this link]。Jsoup獲取部分頁面

File f = new File(projectLocation+logFile+"bidrivalsHome"); 
    try { 
     f.createNewFile(); 
     log.warn("Trying to fetch mainpage through a console."); 
     WinRedirect.redirect(projectLocation+"Curl.exe -s --data \"url="+website+"&delay="+timeDelay+"\" http://127.0.0.1:10000", projectLocation, logFile+"bidrivalsHome"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     log.warn("Error in fetching the nameList", e); 
    } 
    Document doc = new Document(""); 
    try { 
     doc = Jsoup.parse(f, "UTF-8", website); 
    } catch (IOException e1) { 
     System.out.println("Error while parsing the document."); 
     e1.printStackTrace(); 
     log.warn("Error in parsing homepage", e1); 
    } 
+0

你可以發佈你正在使用的生成'...'的代碼? – 2011-06-16 06:46:36

+0

添加了代碼。此外,同樣的事情通過jSoup.connect(url).get() – sumit 2011-06-16 07:08:01

+0

@submit展現出來:但是在這裏你已經構造了文檔。出現在哪裏? – 2011-06-16 07:23:32

回答

1

嘗試使用HtmlUnit來渲染頁面(包括JavaScript和CSS DOM操作),然後通過HTML渲染到jsoup。

// load page using HTML Unit and fire scripts 
WebClient webClient = new WebClient(); 
HtmlPage myPage = webClient.getPage(myURL); 

// convert page to generated HTML and convert to document 
Document doc = Jsoup.parse(myPage.asXml(), baseURI); 

// clean up resources   
webClient.close(); 


page.html中 - 源碼

<html> 
<head> 
    <script src="loadData.js"></script> 
</head> 
<body onLoad="loadData()"> 
    <div class="container"> 
     <table id="data" border="1"> 
      <tr> 
       <th>col1</th> 
       <th>col2</th> 
      </tr> 
     </table> 
    </div> 
</body> 
</html> 

loadData.js

// append rows and cols to table.data in page.html 
    function loadData() { 
     data = document.getElementById("data"); 
     for (var row = 0; row < 2; row++) { 
      var tr = document.createElement("tr"); 
      for (var col = 0; col < 2; col++) { 
       td = document.createElement("td"); 
       td.appendChild(document.createTextNode(row + "." + col)); 
       tr.appendChild(td); 
      } 
      data.appendChild(tr); 
     } 
    } 

page.html當加載到瀏覽器時

| Col1 | Col2 | | ------ | ------ | | 0.0 | 0.1 | | 1.0 | 1.1 |

使用jsoup解析的山坳數據

// load source from file 
    Document doc = Jsoup.parse(new File("page.html"), "UTF-8"); 

    // iterate over row and col 
    for (Element row : doc.select("table#data > tbody > tr")) 

     for (Element col : row.select("td")) 

      // print results 
      System.out.println(col.ownText()); 

輸出

(空)

發生了什麼事page.html中?

Jsoup解析從服務器交付的源代碼(或者在這種情況下從文件加載)。它不會調用JavaScript或CSS DOM操作等客戶端操作。在這個例子中,行和列從不附加到數據表中。

如何解析我的頁面在瀏覽器中呈現?

// load page using HTML Unit and fire scripts 
    WebClient webClient = new WebClient(); 
    HtmlPage myPage = webClient.getPage(new File("page.html").toURI().toURL()); 

    // convert page to generated HTML and convert to document 
    doc = Jsoup.parse(myPage.asXml()); 

    // iterate row and col 
    for (Element row : doc.select("table#data > tbody > tr")) 

     for (Element col : row.select("td")) 

      // print results 
      System.out.println(col.ownText()); 

    // clean up resources   
    webClient.close(); 

輸出

0.0 
0.1 
1.0 
1.1