查询MediaWiki API以获取页面上的链接不会返回所有链接

时间:2018-02-02 13:32:50

标签: javascript mediawiki mediawiki-api

我希望获得此WikiQuote页面上的链接。我想看看基本'基础之下存在哪些子类别?类别。它们在页面上显示为链接,因此向API询问链接似乎很自然。我只回到"类别计划"和"主页"链接,存在于介绍中。我做错了什么/我在这里误解了什么?

CODE

function httpGetAsync(theUrl, callback){
    xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
            callback(xmlHttp.responseText);
        } 
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous
    xmlHttp.send('null');
}

function callback(json_response){
    stuff = json_response;
    console.log(JSON.stringify(JSON.parse(json_response), null, 2));
}

httpGetAsync('http://en.wikiquote.org/w/api.php?action=query&prop=links&titles=Category:Fundamental&origin=*&format=json', callback);

输出

{
  "batchcomplete": "",
  "query": {
    "pages": {
      "4480": {
        "pageid": 4480,
        "ns": 14,
        "title": "Category:Fundamental",
        "links": [
          {
            "ns": 4,
            "title": "Wikiquote:Category schemes"
          },
          {
            "ns": 14,
            "title": "Category:Main page"
          }
        ]
      }
    }
  }
}

Intro links returned, subcategory links are not

1 个答案:

答案 0 :(得分:1)

<强>解决方案

httpGetAsync('https://en.wikiquote.org/w/api.php?&action=query&list=categorymembers&cmtitle=Category:Fundamental&cmtype=subcat&origin=*&format=json', callback);

API documentation for the query used in the solution.

<强>解释

这是要求Fundamental类别页面中的前10个(cmlimit未指定,因此默认为10个返回的项目)子类别。

解决方案通过返回我之后的子类别来解决问题,而不是要求链接。我不确定为什么他们没有出现作为链接,但它确实让我得到了我最后的结果。

<强>积分

感谢FreeCodeCamp论坛上的randelldawson提供此解决方案。

相关问题