jsoup - 从维基百科文章中提取文本

发布于 2025-01-02 11:06:40 字数 181 浏览 3 评论 0原文

我正在编写一些 Java 代码,以便使用维基百科的文本实现 NLP 任务。如何使用 JSoup 提取维基百科文章的所有文本(例如 http:// 中的所有文本en.wikipedia.org/wiki/Boston)?

I'm writing some Java code in order to realize NLP tasks upon texts using Wikipedia. How can I use JSoup to extract all the text of a Wikipedia article (for example all the text in http://en.wikipedia.org/wiki/Boston)?

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

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

发布评论

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

评论(3

╰沐子 2025-01-09 11:06:40
Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Boston").get();
Element contentDiv = doc.select("div[id=content]").first();
contentDiv.toString(); // The result

当然,您可以通过这种方式检索格式化内容。如果您想要“原始”内容,可以使用 Jsoup.clean 过滤结果或使用调用 contentDiv.text()

Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Boston").get();
Element contentDiv = doc.select("div[id=content]").first();
contentDiv.toString(); // The result

You retrieve formatted content this way, of course. If you want "raw" content you can filter the result with Jsoup.clean or use the call contentDiv.text().

半葬歌 2025-01-09 11:06:40
Document doc = Jsoup.connect(url).get();
    Elements paragraphs = doc.select(".mw-content-ltr p");

    Element firstParagraph = paragraphs.first();
    Element lastParagraph = paragraphs.last();
    Element p;
    int i=1;
    p=firstParagraph;
    System.out.println(p.text());
    while (p!=lastParagraph){
        p=paragraphs.get(i);
        System.out.println(p.text());
        i++;
    } 
Document doc = Jsoup.connect(url).get();
    Elements paragraphs = doc.select(".mw-content-ltr p");

    Element firstParagraph = paragraphs.first();
    Element lastParagraph = paragraphs.last();
    Element p;
    int i=1;
    p=firstParagraph;
    System.out.println(p.text());
    while (p!=lastParagraph){
        p=paragraphs.get(i);
        System.out.println(p.text());
        i++;
    } 
骑趴 2025-01-09 11:06:40
Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Boston").timeout(5000);

Element iamcontaningIDofintendedTAG= doc.select("#iamID") ;

System.out.println(iamcontaningIDofintendedTAG.toString());

或者

Elements iamcontaningCLASSofintendedTAG= doc.select(".iamCLASS") ;

System.out.println(iamcontaningCLASSofintendedTAG.toString());
Document doc = Jsoup.connect("http://en.wikipedia.org/wiki/Boston").timeout(5000);

Element iamcontaningIDofintendedTAG= doc.select("#iamID") ;

System.out.println(iamcontaningIDofintendedTAG.toString());

OR

Elements iamcontaningCLASSofintendedTAG= doc.select(".iamCLASS") ;

System.out.println(iamcontaningCLASSofintendedTAG.toString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文