【问题标题】:how to parse two tables from HTML using jsoup?如何使用 jsoup 从 HTML 中解析两个表?
【发布时间】:2013-01-04 16:48:12
【问题描述】:

我需要使用来自站点 http://www.informatik.uni-trier.de/~ley/pers/hd/k/Kumar:G=_Praveen.html 的 jsoup 库来解析 HTML 中的两个表 .. 由于页面上有两个表格,我不知道如何解析表格内容。我需要提取第一个表格的内容,即只有作者姓名及其出版物和最后命名的第二个表格合著者…… 我尝试编码(下面给出的代码)但它给出了错误......

public class Main {
    public static void main(String[] args) {
        try {
            Document doc =Jsoup.connect(“http://www.informatik.unitrier.de/~ley/pers/hd/k/Kumar:G=_Praveen.html“).get();
            Elements trs = doc.select(“table tr”);
            Element table = doc.select(“table[class=coauthor]“).first();
            Iterator ite = table.select(“td”).iterator();
            ite.next();
            System.out.println(“Value 1: ” + ite.next().text());
            System.out.println(“Value 2: ” + ite.next().text());
            System.out.println(“Value 3: ” + ite.next().text());
            System.out.println(“Value 4: ” + ite.next().text());
            trs.remove(0);
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag(“td”);
                Element td = tds.first();
                System.out.println(“Blog: ” + td.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请建议我在上面的代码中究竟需要做哪些更改,以便我从表格中获得我需要的确切信息。任何帮助将不胜感激..thanx 提前..

【问题讨论】:

    标签: java jsoup


    【解决方案1】:

    作者和der出版物:

    final String url = "http://www.informatik.uni-trier.de/~ley/pers/hd/k/Kumar:G=_Praveen.html";
    Document doc = Jsoup.connect(url).get();
    
    
    for( Element element : doc.select("table div.data") )
    {
        // System.out.println(element); // Use this line if you need the HTML Element instead of the text
        System.out.println(element.text());
    }
    

    输出:

    G. Praveen Kumar, Anirban Sarkar: Weighted Association Rule Mining and Clustering in Non-binary Search Space. ITNG 2010: 238-243
    G. Praveen Kumar, Arjun Kumar Murmu, Biswas Parajuli, Prasenjit Choudhury: MULET: A Multilanguage Encryption Technique. ITNG 2010: 779-782
    G. Praveen Kumar, Anirban Sarkar, Narayan C. Debnath: A New Algorithm for Frequent Itemset Generation in Non-Binary Search Space. ITNG 2009: 149-153
    

    合著者:

    for( Element element : doc.select("table td.coauthor") )
    {
        System.out.println(element.text());
    }
    

    输出:

    Prasenjit Choudhury
    Narayan C. Debnath
    Arjun Kumar Murmu
    Biswas Parajuli
    Anirban Sarkar
    

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 2018-11-19
      • 2015-10-14
      • 1970-01-01
      • 2019-06-04
      • 2014-01-01
      • 2019-10-10
      相关资源
      最近更新 更多