【问题标题】:Extract Articles' text from Wikipeda从维基百科中提取文章的文本
【发布时间】:2012-01-09 07:18:41
【问题描述】:

我正在编写一些 java 代码以获取一些维基百科文章的原始文本(给出一个单词 jList,在维基百科中搜索它们并提取相应文章的第一句话)。我的 GUI 包含一个按钮,我为其定义了以下动作侦听器:

private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

final DefaultListModel conceptsListFilesModel = new DefaultListModel();

conceptsList.setModel(conceptsListFilesModel);

final List definitionWiki = new ArrayList();        

//Remplir la list avec la première collone de la liste
final Thread updater = new Thread(){
@Override public void run() {        
for(int i=0; i< 20 /*dataTable.getRowCount()*/ ; i++) {
conceptsListFilesModel.addElement(dataTable.getValueAt(i, 0));

try {
Object concept = conceptsListFilesModel.elementAt(i);
WikipediaParser parser = new WikipediaParser("en");
System.out.println(concept+"");
String firstParagraph = parser.fetchFirstParagraph(concept+"");
int point = firstParagraph.indexOf(".");
String firstsentence = firstParagraph.substring(0, point+1);
definitionWiki.add(i, firstsentence) ;
} catch (IOException ex) {
Logger.getLogger(Tex2TaxView.class.getName()).log(Level.SEVERE, null, ex);
}

try { Thread.sleep(1000);
} catch (InterruptedException e) {throw new RuntimeException(e) ;}
}
JOptionPane.showMessageDialog(null, "Successful loading !")  ;
}
};
updater.start(); 
} 

WikipediaParser 类:

public class WikipediaParser {

private final String baseUrl; 

public WikipediaParser(String lang) {
this.baseUrl = String.format("http://%s.wikipedia.org/wiki/", lang);
}

public String fetchFirstParagraph(String article) throws IOException {
String url = baseUrl + article;
Document doc = Jsoup.connect(url).get();
Elements paragraphs = doc.select(".mw-content-ltr p");
Element firstParagraph = paragraphs.first();
return firstParagraph.text();
}

}

执行会生成以下异常列表:

nov. 30, 2011 12:42:55 AM tex2tax.Tex2TaxView$11 run
Grave: null java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)

at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:641)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589)
at  
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1319)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:381)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
at tex2tax.WikipediaParser.fetchFirstParagraph(WikipediaParser.java:25)
at tex2tax.Tex2TaxView$11.run(Tex2TaxView.java:595)

需要帮助解决这个问题

【问题讨论】:

  • 维基百科没有任何类型的 API 吗?让我感到惊讶。还是那不祥的WikipediaParser?似乎缺少信息:)
  • 我尝试使用 JWPL,但它对我不起作用。所以我更喜欢访问在线维基百科。 WikipediaParser 是我为了使用 jSoup 解析文本而编写的一个类。
  • 添加你的 WikipediaParser 类
  • 需要通过http阅读文章吗?如果没有,您总是可以从dumps.wikimedia.org 获取所有维基百科文章的最新转储并解析这些...
  • 奇怪的是该项目有时会工作,并且大多数时候都会显示错误消息。不知道是不是网速的原因。因为昨天,连接很好,应用程序运行良好。今天连接慢,项目报错。

标签: java nlp jsoup wikipedia


【解决方案1】:

确保您的网址正确无误。连接超时通常意味着存在一些连接问题。

如果您向维基百科提出了很多请求,您可能会被阻止。

您还应该使用Wikipedia API 而不是请求和解析网页。这将比请求和解析 HTML 快得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多