【问题标题】:Querying MediaWiki API for links on a page does not return all links查询页面上链接的 MediaWiki API 不会返回所有链接
【发布时间】:2018-02-02 13:32:50
【问题描述】:

我正在寻找此 WikiQuote 页面上的链接。我想看看“基本”类别下存在哪些子类别。它们在页面上显示为链接,因此向 API 请求链接似乎很自然。我只取回介绍中存在的“类别方案”和“主页”链接。我做错了什么/我在这里误解了什么?

代码

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

【问题讨论】:

    标签: javascript mediawiki mediawiki-api


    【解决方案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.

    说明

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

    该解决方案通过返回我所追求的子类别来解决问题,而不是要求链接。我不确定为什么它们没有显示为链接,但它确实让我得到了我想要的最终结果。

    学分

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

    【讨论】:

    • 因为分类页面中列出的页面是分类的链接。 prop=links API 查询返回来自类别描述的链接。
    猜你喜欢
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多