2012-02-05 63 views
2

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

+1

是解析与有趣的问题'jsoup'部分的文本?因为如果不是的话,你应该使用'action = raw'参数来获取每个页面的源代码。例如http://en.wikipedia.org/w/index.php?title=Elephant&action=raw – beerbajay 2012-02-05 16:59:59

+0

返回Wiki标记。 – 2012-02-05 17:30:42

+0

使用它,它在维基百科服务器上也更加强大和安全:http://trulymadlywordly.blogspot.com/2011/03/creating-text-corpus-from-wikipedia.html – Maarten 2013-09-08 01:53:21

回答

3
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()

+0

为什么不呢?它应该,但它返回像标题或枚举项目符号。如果您只需要(英文)单词,您需要过滤一下,但我们不知道您的具体要求。 – 2012-02-06 13:18:26

+0

我在你的代码中复制了你的代码,但它不工作。然而,在尝试您的解决方案之前,我已经自己完成了......并且解决方案也是类似的不过谢谢...现在我正在处理另一个问题! http://stackoverflow.com/questions/9160760/how-can-i-extract-specific-links-in-wikipedia-articles-using-jsoup#comment11521257_9160760 – Ema 2012-02-06 14:07:21

+0

我从实时代码中取出这些行,所以它们应该至少运行。 – 2012-02-06 14:52:03

0
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()); 
+0

谢谢...现在正在工作! – Ema 2012-02-07 00:04:51

+0

:)那很好 – 2012-02-09 14:18:36

+0

:) 对!!!!!!!!!!!! – Ema 2012-02-09 18:11:17

2
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++; 
    }