代码之家  ›  专栏  ›  技术社区  ›  serendipity

使用jsoup从Wikipedia获取目录(文本和层次结构)

  •  0
  • serendipity  · 技术社区  · 7 年前

    下面是我用来获取维基百科特定主题的“内容”部分的代码。我还需要帮助获得文本的层次结构,然后将其添加到地图中。E、 g.如果我们搜索咖啡,我们会得到:

    1   Etymology
    2   History
    2.1 Legendary accounts
    2.2 Historical transmission
    3   Biology
    4   Cultivation
    4.1 Ecological effects
    5   Production
    6   Processing
    6.1 Roasting
    6.2 Grading roasted beans
    6.3 Roast characteristics
    6.4 Decaffeination
    6.5 Storage
    

    我想保留层次结构(4,4.1),即父节点和相应的子节点以及文本,并将它们作为键值对添加到hashmap中。如何使用我的代码做到这一点?

    public static void getWikiNodesForTopic(String url) throws IOException {
    
    
           Response res = Jsoup.connect(url)
                .execute();
    
            String html = res.body();
            Document doc = Jsoup.parseBodyFragment(html);
    
            Elements elements = doc.body().select(".toctext");
    
            for (Element element : elements) {
    
                if (element.text().contentEquals("See also") || element.text().contentEquals("References") || element.text().contentEquals("Bibliography") || element.text().contentEquals("External links") || element.text().contentEquals("Bibliography"))
                    continue;
    
                else
                    //System.out.println(element.select(".tocnumber"));
                    System.out.println(element.ownText());
            }
    
         }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Developer Guy Adam    7 年前

    由于目录值是所需文本值的同级,因此可以执行以下操作:

    for (Element element : elements) {
    
        if (element.text().contentEquals("See also") || element.text().contentEquals("References") || element.text().contentEquals("Bibliography") || element.text().contentEquals("External links") || element.text().contentEquals("Bibliography"))
            continue;
    
        else
            // This will get the element immediately before the current one.
            System.out.print(element.previousElementSibling().text() + " ");
            System.out.println(element.ownText());
    }