是否有一个干净的维基百科API仅用于检索内容摘要?

我只需要检索维基百科页面的第一段。 内容必须是html格式,准备在我的网站上显示(所以没有BBCODE,或WIKIPEDIA特殊代码!)

有一种方法来获得整个“介绍部分”没有任何HTMLparsing! 与AnthonyS的答案类似,使用附加的explaintext参数,您可以以纯文本forms获取介绍部分文本。

询问

以纯文本forms获取堆栈溢出的介绍:

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Stack%20Overflow

JSON响应

(警告被剥离)

 { "query": { "pages": { "21721040": { "pageid": 21721040, "ns": 0, "title": "Stack Overflow", "extract": "Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky, as a more open alternative to earlier Q&A sites such as Experts Exchange. The name for the website was chosen by voting in April 2008 by readers of Coding Horror, Atwood's popular programming blog.\nIt features questions and answers on a wide range of topics in computer programming. The website serves as a platform for users to ask and answer questions, and, through membership and active participation, to vote questions and answers up or down and edit questions and answers in a fashion similar to a wiki or Digg. Users of Stack Overflow can earn reputation points and \"badges\"; for example, a person is awarded 10 reputation points for receiving an \"up\" vote on an answer given to a question, and can receive badges for their valued contributions, which represents a kind of gamification of the traditional Q&A site or forum. All user-generated content is licensed under a Creative Commons Attribute-ShareAlike license. Questions are closed in order to allow low quality questions to improve. Jeff Atwood stated in 2010 that duplicate questions are not seen as a problem but rather they constitute an advantage if such additional questions drive extra traffic to the site by multiplying relevant keyword hits in search engines.\nAs of April 2014, Stack Overflow has over 2,700,000 registered users and more than 7,100,000 questions. Based on the type of tags assigned to questions, the top eight most discussed topics on the site are: Java, JavaScript, C#, PHP, Android, jQuery, Python and HTML." } } } } 

文档: API:query / prop =提取

实际上有一个非常好的道具称为提取 ,可以用于专门为此目的devise的查询。 提取物允许您获取文章摘录(截断文章文本)。 有一个名为exintro的参数,可以用来检索第零段中的文本 (没有像图像或信息框这样的附加资源)。 您也可以检索更细粒度的提取物,例如一定数量的字符( 交换者 )或一定数量的句子( extences

这里是一个示例查询 http://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=Stack%20Overflow和;API沙箱 http://en.wikipedia.org/wiki/ Special:ApiSandbox#action = query&prop = extracted&format = json&exintro =&titles = Stack%20Overflow对此查询进行更多的实验。

请注意,如果你想特别的第一段,你仍然需要做一些额外的parsing所build议的select答案。 这里的区别是这个查询返回的响应比其他一些api查询build议的要短,因为你没有其他的资源,比如api响应中的图像parsing。

此代码允许您以纯文本格式检索页面第一段的内容。

这个答案的一部分来自这里 ,因此在这里 。 有关更多信息,请参阅MediaWiki API文档 。

 // action=parse: get parsed text // page=Baseball: from the page Baseball // format=json: in json format // prop=text: send the text content of the article // section=0: top content of the page $url = 'http://en.wikipedia.org/w/api.php?format=json&action=parse&page=Baseball&prop=text&section=0'; $ch = curl_init($url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript"); // required by wikipedia.org server; use YOUR user agent with YOUR contact information. (otherwise your IP might get blocked) $c = curl_exec($ch); $json = json_decode($c); $content = $json->{'parse'}->{'text'}->{'*'}; // get the main text content of the query (it's parsed HTML) // pattern for first match of a paragraph $pattern = '#<p>(.*)</p>#Us'; // http://www.phpbuilder.com/board/showthread.php?t=10352690 if(preg_match($pattern, $content, $matches)) { // print $matches[0]; // content of the first paragraph (including wrapping <p> tag) print strip_tags($matches[1]); // Content of the first paragraph without the HTML tags. } 

就在这里。 例如,如果您想获得文章Stack Overflow第一部分的内容,请使用如下查询:

http://en.wikipedia.org/w/api.php?format=xml&action=query&prop=revisions&titles=Stack%20Overflow&rvprop=content&rvsection=0&rvparse

这些部分是指:

  • format=xml :将结果格式化程序作为XML返回。 其他选项(如JSON)可用。 这并不影响页面内容本身的格式,只是封闭的数据格式。

  • action=query&prop=revisions :获取有关页面修订的信息。 由于我们没有指定哪个版本,所以使用最新版本。

  • titles=Stack%20Overflow :获取关于页面Stack Overflow 。 如果用|将他们的名字分开,可以一次获得更多页面的文本 。

  • rvprop=content :返回版本的内容(或文本)。

  • rvsection=0 :只返回第0部分的内容。

  • rvparse :返回parsing为HTML的内容。

请记住,这将返回整个第一部分,包括像hatnotes(“其他用途…”),信息框或图像的东西。

有几种库可用于各种语言,使API工作更容易,如果您使用其中之一,可能会更好。

这是我正在使用的一个网站,我正在使用的代码,需要得到维基百科文章的主要段落/摘要/第0部分,这一切都在浏览器(客户端JavaScript)感谢magick JSONP! – > http://jsfiddle.net/gautamadude/HMJJg/1/

它使用维基百科API获取HTML中的前导段落(称为第0节),如下所示: http : //en.wikipedia.org/w/api.php?format=json&action=parse&page=Stack_Overflow&prop=text& section=0& callback=?

然后剥离HTML和其他不需要的数据,给你一个干净的string的文章总结,如果你想你可以,稍微调整,得到一个“p”的HTML标签周围的领导段落,但现在只是一个换行符他们之间的性格。

码:

 var url = "http://en.wikipedia.org/wiki/Stack_Overflow"; var title = url.split("/").slice(4).join("/"); //Get Leading paragraphs (section 0) $.getJSON("http://en.wikipedia.org/w/api.php?format=json&action=parse&page=" + title + "&prop=text&section=0&callback=?", function (data) { for (text in data.parse.text) { var text = data.parse.text[text].split("<p>"); var pText = ""; for (p in text) { //Remove html comment text[p] = text[p].split("<!--"); if (text[p].length > 1) { text[p][0] = text[p][0].split(/\r\n|\r|\n/); text[p][0] = text[p][0][0]; text[p][0] += "</p> "; } text[p] = text[p][0]; //Construct a string from paragraphs if (text[p].indexOf("</p>") == text[p].length - 5) { var htmlStrip = text[p].replace(/<(?:.|\n)*?>/gm, '') //Remove HTML var splitNewline = htmlStrip.split(/\r\n|\r|\n/); //Split on newlines for (newline in splitNewline) { if (splitNewline[newline].substring(0, 11) != "Cite error:") { pText += splitNewline[newline]; pText += "\n"; } } } } pText = pText.substring(0, pText.length - 2); //Remove extra newline pText = pText.replace(/\[\d+\]/g, ""); //Remove reference tags (ex [1], [4], etc) document.getElementById('textarea').value = pText document.getElementById('div_text').textContent = pText } }); 

此url将以xml格式返回摘要。

 http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=Agra&MaxHits=1 

我创build了一个函数来获取wikipedia中关键字的描述。

 function getDescription($keyword){ $url='http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString='.urlencode($keyword).'&MaxHits=1'; $xml=simplexml_load_file($url); return $xml->Result->Description; } echo getDescription('agra'); 

您还可以通过DBPedia获取第一个pagagraph的内容,这个维基百科的内容可以从中获取结构化信息(RDF),并通过API提供。 DBPedia API是一个SPARQL(基于RDF的),但它输出JSON,并且很容易打包。

作为一个例子,这里有一个名为WikipediaJS的超级简单的JS库,它可以提取结构化的内容,包括一个总结第一段: http : //okfnlabs.org/wikipediajs/

你可以阅读更多关于它在这篇博客文章: http : //okfnlabs.org/blog/2012/09/10/wikipediajs-a-javascript-library-for-accessing-wikipedia-article-information.html

JS库的代码可以在这里find: https : //github.com/okfn/wikipediajs/blob/master/wikipedia.js

abstract.xml.gz转储听起来像你想要的。

我的方法如下(在PHP中):

 $url = "whatever_you_need" $html = file_get_contents('https://en.wikipedia.org/w/api.php?action=opensearch&search='.$url); $utf8html = html_entity_decode(preg_replace("/U\+([0-9A-F]{4})/", "&#x\\1;", $html), ENT_NOQUOTES, 'UTF-8'); 

$utf8html可能需要进一步清理,但基本上是这样。

如果您只是在寻找可以拆分的文本,但不想使用API​​,请查看en.wikipedia.org/w/index.php?title=Elephant&action=raw

我试了@Michael Rapadas和@ Krinkle的解决scheme,但在我的情况下,我很难根据大小写find一些文章。 像这儿:

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&exsentences=1&explaintext=&titles=Led%20zeppelin

注意我截断了exsentences=1的回应

显然“标题正常化”不能正常工作:

标题规范化将页面标题转换为规范forms。 这意味着大写第一个字符,用空格replace下划线,并将名称空间更改为为该wiki定义的本地化表单。 标题规范化是自动完成的,不pipe使用哪个查询模块。 但是,页面标题(\ n)中的任何尾随行会导致奇怪的行为,并且应该先剥离它们。

我知道我可以很容易地整理大写字母的问题,但是也不便于将对象转换为数组。

所以,因为我真的很想要知道和定义的search的第一段(没有风险从另一篇文章中获取信息),我这样做:

https://en.wikipedia.org/w/api.php?action=opensearch&search=led%20zeppelin&limit=1&format=json

注意在这种情况下,我做了limit=1的截断

这条路:

  1. 我可以很容易地访问响应数据。
  2. 反应是相当小的。

但是,我们必须保持对search资本化的谨慎。

更多信息: https : //www.mediawiki.org/wiki/API : Opensearch