是否有一个 Wikipedia API 仅用于检索内容摘要?

我只需要检索维基百科页面的第一段。

内容必须是 HTML 格式,准备在我的网站上显示(所以 没有 BBCode,或 维基百科特别 密码!)

158801 次浏览

abstract.xml.gz垃圾场听起来就是你想要的。

有的。例如,如果您希望获得文章 堆栈溢出第一部分的内容,可以使用如下查询:

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: 获取关于页面修订的信息。由于我们没有指定哪个修订,所以使用最新的一个。

  • 获取有关页面 Stack Overflow的信息。如果你用 |分隔它们的名字,就有可能一次性得到更多页面的文本。

  • 返回修订版的内容(或文本)。

  • rvsection=0: 仅返回第0节中的内容。

  • 返回解析为 HTML 的内容。

请记住,这将返回整个第一部分,包括诸如帽子笔记(“其他用途...”)、信息框或图像等内容。

有几个可用于各种语言的库,它们使得使用 API 变得更加容易,如果您使用其中之一,可能会更好。

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

这个答案的一部分来自于 给你,因此也来自于 给你。更多信息参见 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.
}

如果您只是在寻找文本,然后可以将其拆分,但是不想使用 API,那么可以查看 En.wikipedia.org/w/index.php?title=elephant&action=raw

此 URL 将以 XML 格式返回摘要。

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

我已经创建了一个函数来从 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获取内容,比如第一段,它接受 Wikipedia 内容并从中创建结构化信息(RDF) ,并通过 API 提供这些内容。DBPedia API 是一个 SPARQL API (基于 RDF) ,但是它输出 JSON 并且很容易包装。

这里有一个名为 维基百科的超级简单的 JavaScript 库,它可以提取包括摘要第一段在内的结构化内容。

你可以在这篇博文 WikipediaJS-通过 Javascript 访问 Wikipedia 文章数据中了解更多

JavaScript 库代码可以在 维基百科中找到。

这是我现在正在使用的代码,我正在制作的网站,需要获得的前导段落,摘要,和第0部分的维基百科文章,这一切都是在浏览器(客户端 JavaScript)内完成的感谢神奇的 JSONP!—— > http://jsfiddle.net/gautamadude/HMJJg/1/

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

然后删除 HTML 和其他不需要的数据,为您提供一个简洁的文章摘要字符串。如果需要,稍微调整一下,可以在前面的段落周围添加一个“ 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 (e.x. [1], [4], etc)
document.getElementById('textarea').value = pText
document.getElementById('div_text').textContent = pText
}
});

实际上有一个非常好的 道具,称为 翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳,可以用于专门为此目的设计的查询。

提取 允许您获取文章提取(截断的文章文本)。有一个名为 外景介绍的参数可用于 检索第零部分中的文本(没有其他资产,如图像或信息箱)。您还可以检索具有更细粒度的提取,例如按照一定数量的字符(Exchars)或按照一定数量的句子(句子)。

下面是一个 示例查询 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=extracts&format=json&exintro=&titles=Stack%20Overflow对这个查询进行更多的实验。

请注意,如果您特别需要第一段,您仍然需要按照所选答案中的建议进行一些额外的解析。这里的不同之处在于,这个查询返回的响应比其他一些 API 查询建议的响应要短,因为在 API 响应中没有要解析的其他资产,比如图像。

来自医生的警告:

我们不推荐使用 exsentences。它不适用于 HTML 提取,并且有许多边界情况下它不存在。例如「手臂」。将军。约翰 · 史密斯是个军人”将被视为四句话。我们不打算解决这个问题。

有一种方法可以获得整个“介绍部分”,而不需要任何 HTML 解析!类似于带有额外 explaintext参数的 Anthony 的回答,您可以获得纯文本的介绍部分文本。

质疑

获取 Stack Overflow 的纯文本介绍:

使用页面标题:

Https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=stack%20overflow

或使用 pageids:

Https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&pageids=21721040

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."
}
}
}
}

文件: 查询/道具 = 提取

我的方法如下(在 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可能需要进一步清洗,但基本上就是这样了。

我尝试了 迈克尔 · 拉帕达斯的和@Kinkle 的解决方案,但在我的情况下,我很难找到一些文章取决于大小写。比如这里:

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

注意,我用 exsentences=1截断了响应

显然,“头衔正常化”并没有起到正确的作用:

标题规范化将页面标题转换为它们的规范形式 意味着将第一个字符大写,将下划线替换为 空间,并将命名空间更改为为此定义的本地化形式 标题规范化是自动完成的,不管是哪个 使用查询模块。但是,页中的任何尾行都会中断 标题(n)会导致奇怪的行为,应该把它们去掉 第一。

我知道我可以很容易地解决大小写问题,但是还有一个不便之处,那就是必须将对象强制转换为数组。

因为我只是非常想要一个众所周知的、定义明确的搜索的第一段(没有从其他文章中获取信息的风险) ,我是这样做的:

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

注意,在本例中,我使用 limit=1进行了截断

这边走:

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

但我们必须对搜索的资本化保持谨慎。

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

自2017年以来,Wikipedia 提供了具有更好缓存的 REST API。在 文件中,您可以找到以下完全适合您的用例的 API (因为它被新的 页面预览特性所使用)。

https://en.wikipedia.org/api/rest_v1/page/summary/Stack_Overflow 返回下列可用于显示小缩略图摘要的数据:

{
"type": "standard",
"title": "Stack Overflow",
"displaytitle": "Stack Overflow",
"extract": "Stack Overflow is a question and answer site for professional and enthusiast programmers. It is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky. It features questions and answers on a wide range of topics in computer programming. It was created to be a more open alternative to earlier question and answer 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.",
"extract_html": "<p><b>Stack Overflow</b> is a question and answer site for professional and enthusiast programmers. It is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky. It features questions and answers on a wide range of topics in computer programming. It was created to be a more open alternative to earlier question and answer sites such as Experts-Exchange. The name for the website was chosen by voting in April 2008 by readers of <i>Coding Horror</i>, Atwood's popular programming blog.</p>",
"namespace": {
"id": 0,
"text": ""
},
"wikibase_item": "Q549037",
"titles": {
"canonical": "Stack_Overflow",
"normalized": "Stack Overflow",
"display": "Stack Overflow"
},
"pageid": 21721040,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/en/thumb/f/fa/Stack_Overflow_homepage%2C_Feb_2017.png/320px-Stack_Overflow_homepage%2C_Feb_2017.png",
"width": 320,
"height": 149
},
"originalimage": {
"source": "https://upload.wikimedia.org/wikipedia/en/f/fa/Stack_Overflow_homepage%2C_Feb_2017.png",
"width": 462,
"height": 215
},
"lang": "en",
"dir": "ltr",
"revision": "902900099",
"tid": "1a9cdbc0-949b-11e9-bf92-7cc0de1b4f72",
"timestamp": "2019-06-22T03:09:01Z",
"description": "website hosting questions and answers on a wide range of topics in computer programming",
"content_urls": {
"desktop": {
"page": "https://en.wikipedia.org/wiki/Stack_Overflow",
"revisions": "https://en.wikipedia.org/wiki/Stack_Overflow?action=history",
"edit": "https://en.wikipedia.org/wiki/Stack_Overflow?action=edit",
"talk": "https://en.wikipedia.org/wiki/Talk:Stack_Overflow"
},
"mobile": {
"page": "https://en.m.wikipedia.org/wiki/Stack_Overflow",
"revisions": "https://en.m.wikipedia.org/wiki/Special:History/Stack_Overflow",
"edit": "https://en.m.wikipedia.org/wiki/Stack_Overflow?action=edit",
"talk": "https://en.m.wikipedia.org/wiki/Talk:Stack_Overflow"
}
},
"api_urls": {
"summary": "https://en.wikipedia.org/api/rest_v1/page/summary/Stack_Overflow",
"metadata": "https://en.wikipedia.org/api/rest_v1/page/metadata/Stack_Overflow",
"references": "https://en.wikipedia.org/api/rest_v1/page/references/Stack_Overflow",
"media": "https://en.wikipedia.org/api/rest_v1/page/media/Stack_Overflow",
"edit_html": "https://en.wikipedia.org/api/rest_v1/page/html/Stack_Overflow",
"talk_page_html": "https://en.wikipedia.org/api/rest_v1/page/html/Talk:Stack_Overflow"
}
}

默认情况下,它遵循重定向(这样 /api/rest_v1/page/summary/StackOverflow也可以工作) ,但是可以使用 ?redirect=false禁用它。

如果需要从另一个域访问 API,可以使用 &origin=(例如,&origin=*)设置 CORS头。

截至2019年: API 似乎返回了更多关于页面的有用信息。