【问题标题】:Using MediaWiki API with the Continue Command使用带有继续命令的 MediaWiki API
【发布时间】:2013-06-22 04:59:44
【问题描述】:

我需要一些帮助 使用带有“继续”或“查询继续”命令的 Mediawiki API 从我的 wiki 文章中提取信息。我有大量的 wiki 文章(目前超过 800 篇),我需要使用 api 将它们分批提取 50 篇,然后打印 ou 部分。

我的 API 调用正常:

//Stackoverflow 让我在这里使用有效的 URL,这个 api 实际上是我自己的 localhost 服务器 http://en.wikipedia.org/w/api.php?action=query&list=allpages&apfrom=a&apto=z&apnamespace=0&format=xml&aplimit=50我正在查询所有页面,因此是“apfrom”和“apto”。

我只需要帮助处理代码,使用 PHP 和 CURL 访问 API 并处理 50 个批次并使用“继续”访问更多记录,直到我结束。到目前为止,我的 php 代码是:

  //the CURL commands here work and outputs a data set but only for the first 50 records, so I need to call "continue" to get to the end.  
 //My api url is localhost but I'm forced to use a valid URL by Stackoverflow.com
$url = sprintf('http://en.wikipedia.org/w/api.php?                                                                                            
 action=query&list=allpages&apfrom=a&apto=z&apnamespace=0&format=xml&aplimit=50');
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'My site');
$res = curl_exec($ch);

$continue = '';

while ( // I don't know what to set here as true to get the while loop going, maybe continue                      = true? maybe set query-continue as true?) 
{
    //Maybe I need something other than $res['query-continue]??
    if (empty($res['query-continue'])) 
    {
        exit;
    }
    else 
    {
        $continue = '&apcontinue='.urlencode($res['query-continue']);
            foreach ($res['query']['allpages'] as $v) 
            {
                echo $v['title'];
            }
    }
}

有人可以更正我上面的 while 循环代码,以便我可以简单地打印循环中每篇 wiki 文章的标题吗?我在网上做了很多搜索,但我被卡住了!!我在http://www.mediawiki.org/wiki/API:Query 找到了一个 python 循环示例,但我必须在 PHP 中完成。而且我不确定我是调用 continue 还是 query-continue。

【问题讨论】:

  • 如果要查询所有页面,请不要设置apfromapto。并非所有页面名称都必须以字母开头。
  • 另外,你为什么不使用API libraries之一?
  • 我正在使用 botclasses.php 库,但它没有我需要的查询功能。其他一些 botclass 库不适合我,所以我必须使用 continue 编写自己的函数。

标签: php mediawiki mediawiki-api


【解决方案1】:

正如 svick 所说,请使用 client library 来为您处理继续。

MediaWiki 中的查询延续机制已经多次更改,你不想理解甚至更少依赖它。

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 2016-11-19
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2018-11-26
    相关资源
    最近更新 更多