使用Jsoup提取和解析HTML表格

3

我该如何使用Jsoup从这个网站中提取规格数据,例如每一行分别提取 网络->网络类型、电池等。

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());   
            }
        }
    }
}
4个回答

6
这里尝试找到解决您问题的方法。
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]
  • 前两行仅包含格式化标记,因此跳过这些行tr:gt(2)
  • 每行都以内容类型的全局描述开始,过滤它们td:not([rowspan])
有关选择器语法中更复杂的选项,请查看这里

3

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

针对值的Xpath - //*[@id="phone_details"]/tbody/tr[3]/td[3]

@Joey的代码试图聚焦于这些内容。您应该能够根据Xpath编写select()规则。

请用适当的值替换数字(tr [N] / td [N])。

或者,您可以将HTML通过文本浏览器传送并从文本中提取数据。这是页面的纯文本版本。您可以使用分隔符或在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();
        }
    }
}

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接