如何使用 jsoup 提取维基百科文章中的特定链接?

发布于 2025-01-03 02:03:20 字数 183 浏览 4 评论 0原文

我正在做一个 NLP 项目,我需要知道如何提取仅位于此 wikipidia 页面的“简介”部分和“地理”部分中的链接: http://en.wikipedia.org/wiki/Boston

你能帮我吗?

I am doing an NLP project and I need to know how to extract links that only are in the "introduction" section and in the "geography" section of this wikipidia page: http://en.wikipedia.org/wiki/Boston

Could you please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈甜 2025-01-10 02:03:20

维基百科并没有让这变得容易。我并不认为这是优雅的,甚至不是非常可重用的。

    Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Boston").timeout(5000).get();

    Element intro = doc.body().select("p").first();
    while (intro.tagName().equals("p")) {
        //here you will get an Elements object which you can
        //iterate through to get the links in the intro
        System.out.println(intro.select("a"));
        intro = intro.nextElementSibling();
    }

    for (Element h2 : doc.body().select("h2")) {
        if(h2.select("span").size() == 2) {
            if (h2.select("span").get(1).text().equals("Geography")) {
                Element nextsib = h2.nextElementSibling();
                while (nextsib != null) {
                    if (nextsib.tagName().equals("p")) {
                        //here you will get an Elements object which you
                        //can iterate through to get the links in the 
                        //geography section
                        System.out.println(nextsib.select("a"));
                        nextsib = nextsib.nextElementSibling();
                    } else if (nextsib.tagName().equals("h2")) {
                        nextsib = null;
                    } else {
                        nextsib = nextsib.nextElementSibling();
                    }
                }
            }
        }
    }
}

Wikipedia does not make this easy. I don't claim this to be elegant or even very reuseable.

    Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Boston").timeout(5000).get();

    Element intro = doc.body().select("p").first();
    while (intro.tagName().equals("p")) {
        //here you will get an Elements object which you can
        //iterate through to get the links in the intro
        System.out.println(intro.select("a"));
        intro = intro.nextElementSibling();
    }

    for (Element h2 : doc.body().select("h2")) {
        if(h2.select("span").size() == 2) {
            if (h2.select("span").get(1).text().equals("Geography")) {
                Element nextsib = h2.nextElementSibling();
                while (nextsib != null) {
                    if (nextsib.tagName().equals("p")) {
                        //here you will get an Elements object which you
                        //can iterate through to get the links in the 
                        //geography section
                        System.out.println(nextsib.select("a"));
                        nextsib = nextsib.nextElementSibling();
                    } else if (nextsib.tagName().equals("h2")) {
                        nextsib = null;
                    } else {
                        nextsib = nextsib.nextElementSibling();
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文