如何使用JSoup从iFrame标签中的多个表中提取数据

时间:2013-12-06 15:16:14

标签: java html-table

我需要在iframe标签中提取多个表的内容, 我正在使用此代码

Document doc = Jsoup.connect("http://pubedt.uvsq.fr/VER/g4227.html")
            .header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/19.0").timeout(100*1000)
            .get();
    for (Element table : doc.select("table")) {
    //    System.out.println(table.html());
        for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
            if (tds.size() > 6) {
                System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

看起来你在正确的轨道上,但tds.size()从未超过6个条目:

Document doc = Jsoup
        .connect("http://pubedt.uvsq.fr/VER/g4227.html")
        .header("User-Agent",
                "Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/19.0")
        .timeout(100 * 1000).get();
for (Element table : doc.select("table")) {
    // System.out.println(table.html());
    for (Element row : table.select("tr")) {
        Elements tds = row.select("td");
        // log added here
        System.out.println("tds.size(): " + tds.size());
        if (tds.size() > 6) {
            System.out.println(tds.get(0).text() + ":"
                    + tds.get(1).text());
        }
    }
}

输出:

tds.size(): 6
tds.size(): 1
tds.size(): 1
tds.size(): 1
tds.size(): 0
tds.size(): 1
tds.size(): 1
tds.size(): 1
tds.size(): 1
tds.size(): 1
tds.size(): 0
tds.size(): 1
tds.size(): 1