2013-04-06 46 views
1

我怎樣才能使用Jsoup從this website分別提取每行的規格數據,例如:網絡 - >網絡類型,電池等使用Jsoup提取和解析HTML表格

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class mobilereviews { 
    public static void main(String[] args) throws Exception { 
     Document doc = Jsoup.connect("http://mobilereviews.net/details-for-Motorola%20L7.htm").get(); 
     for (Element table : doc.select("table")) { 
      for (Element row : table.select("tr")) { 
       Elements tds = row.select("td"); 
       System.out.println(tds.get(0).text()); 
      } 
     } 
    } 
} 

回答

2

的XPath列 - //*[@id="phone_details"]/tbody/tr[3]/td[2]/strong

的XPath的價值觀 - //*[@id="phone_details"]/tbody/tr[3]/td[3]

@喬伊的代碼試圖在零上這些。您應該能夠根據Xpath編寫select()規則。

用適當的值替換數字(tr [N]/td [N])。

或者,您可以將HTML視爲一個純文本瀏覽器,並從文本中提取數據。這是頁面的text version。您可以對文本進行分隔或在N個字符之後進行讀取以提取數據。

5

這裏是要找到解決問題的方法

Document doc = Jsoup.connect("http://mobilereviews.net/details-for-Motorola%20L7.htm").get(); 

for (Element table : doc.select("table[id=phone_details]")) { 
    for (Element row : table.select("tr:gt(2)")) { 
     Elements tds = row.select("td:not([rowspan])"); 
     System.out.println(tds.get(0).text() + "->" + tds.get(1).text()); 
    } 
} 

解析HTML是棘手的,如果HTML改變你的代碼需要改變如企圖好。

您需要研究HTML標記以首先提出解析規則。

  • 有在HTML多個表,所以你在正確的table[id=phone_details]
  • 第一過濾器前2點表中的行只包含標記進行格式化,所以跳過它們tr:gt(2)
  • 每隔一行與開始內容類型描述全球,過濾出來td:not([rowspan])

對於選擇語法較爲複雜的選項,看看這裏http://jsoup.org/cookbook/extracting-data/selector-syntax

1

這是我如何從html表中獲取數據。

org.jsoup.nodes.Element tablaRegistros = doc 
        .getElementById("tableId"); 
for (org.jsoup.nodes.Element row : tablaRegistros.select("tr")) { 
       for (org.jsoup.nodes.Element column : row.select("td")) { 
        // Elements tds = row.select("td"); 
        // cadena += tds.get(0).text() + "->" + 
        // tds.get(1).text() 
        // + " \n"; 
        cadena += column.text() + ","; 
       } 
       cadena += "\n"; 
      } 
1

這是通過JSoup從HTML頁面提取表的通用解決方案。

import java.io.IOException; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class ExtractTableDataUsingJSoup { 

    public static void main(String[] args) { 
     extractTableUsingJsoup("http://mobilereviews.net/details-for-Motorola%20L7.htm","phone_details"); 
    } 

    public static void extractTableUsingJsoup(String url, String tableId){ 
     Document doc; 
     try { 
      // need http protocol 
      doc = Jsoup.connect(url).get(); 

      //Set id of any table from any website and the below code will print the contents of the table. 
      //Set the extracted data in appropriate data structures and use them for further processing 
      Element table = doc.getElementById(tableId); 

      Elements tds = table.getElementsByTag("td"); 

      //You can check for nesting of tds if such structure exists 
      for (Element td : tds) { 
       System.out.println("\n"+td.text()); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}