【问题标题】:How to combine two Wikipedia API calls into one?如何将两个 Wikipedia API 调用合二为一?
【发布时间】:2016-03-06 11:46:58
【问题描述】:

我正在调用一个 Wikipedia API,它返回该位置的标题、镜头文本、图像和地理坐标。我的维基百科 API 是:

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts|pageimages|coordinates&titles=Berlin&redirects=1&formatversion=2&exintro=1&explaintext=1&piprop=thumbnail&pithumbsize=400

我还在使用另一个 Wikipedia API,它根据地理坐标返回地名列表:

https://en.wikipedia.org/w/api.php?format=json&action=query&list=geosearch&gsradius=1000&gscoord=52.5243700|13.4105300&gslimit=50&gsprop=type|dim|globe

对于第二个 API,我得到如下响应:

"query": {
    "geosearch": [
        {
            "pageid": 28782169,
            "ns": 0,
            "title": "1757 Berlin raid",
            "lat": 52.523405,
            "lon": 13.4114,
            "dist": 122.4,
            "primary": "",
            "type": null,
            "dim": 1000
        },
        {
            "pageid": 526195,
            "ns": 0,
            "title": "Scheunenviertel",
            "lat": 52.526111111111,
            "lon": 13.41,
            "dist": 196.9,
            "primary": "",
            "type": "landmark",
            "dim": 1000
        },
        ...
    ]
}

现在我想将这两个搜索组合到一个 API 中。我想在第二个 API 中添加我的第一个 API 的信息,如下所示:

"query": {
    "geosearch": [
        {
            "pageid": 28782169,
            "ns": 0,
            "title": "1757 Berlin raid",
            "lat": 52.523405,
            "lon": 13.4114,
            "dist": 122.4,
            "primary": "",
            "type": null,
            "dim": 1000

            "pages": [
                {
                    "pageid": 28782169,
                    "ns": 0,
                    "title": "1757 Berlin raid",
                    "extract": "Berlin is the capital of Germany and one of the 16 states of Germany. With a population of 3.5 million people, it is the second most populous city proper and the seventh.........",
                    "thumbnail": {
                        "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Siegessaeule_Aussicht_10-13_img4_Tiergarten.jpg/400px-Siegessaeule_Aussicht_10-13_img4_Tiergarten.jpg",
                        "width": 400,
                        "height": 267
                    }
                }
            ]
        },
        ...
    ]
}

我想知道这样可以吗?

【问题讨论】:

    标签: mediawiki wikipedia wikipedia-api mediawiki-api


    【解决方案1】:

    所以,如果我理解正确,您希望通过一个 Wikipedia API 请求获得 titleshot-textimage 和 地理坐标(您的第一个 API)。如果正确,您可以这样做:

    1. main parameters:format=json&action=query

    2. query parameters:

      • redirects=1
      • generator=geosearch(您的第二个 API:见第 3 点)
      • prop=extracts|coordinates|pageimages(您的第一个 API:参见第 4、5 和 6 点)
    3. geosearch parameters(所有生成器参数都以“g”为前缀):

      • ggslimit=20 - 查询的总结果(因为 exlimit=20
      • ggsradius=1000&ggscoord=52.5243700|13.4105300 - 这是您的入口点
    4. parameters for extracts:exintro=1&explaintext=1&exlimit=20exlimit 最大为 20)

    5. parameters for coordinates:coprop=type|dim|globe&colimit=20(最大colimit为500)

    6. parameters for pageimages: piprop=thumbnail&pithumbsize=400&pilimit=20(最多 50 个)

    您看,colimit 的最大值为 500,pilimit 的最大值为 50,但我们不能使用超过 20,因为 exlimit

    或者最后,您的请求将是上述所有参数的加入:

    https://en.wikipedia.org/w/api.php?format=json&action=query&redirects=1&generator=geosearch&prop=extracts|coordinates|pageimages&ggslimit=20&ggsradius=1000&ggscoord=52.5243700|13.4105300&exintro=1&explaintext=1&exlimit=20&coprop=type|dim|globe&colimit=20&piprop=thumbnail&pithumbsize=400&pilimit=20
    

    下面是回复:

    "query":{
        "pages":{
            "2511":{
                "pageid":2511,
                "ns":0,
                "title":"Alexanderplatz",
                "extract":"Alexanderplatz (pronounced [\u0294al\u025bk\u02c8sand\u0250\u02ccplats]) is a large public square and transport hub in the central Mitte district of Berlin, near the Fernsehturm. Berliners often call it simply Alex, referring to a larger neighbourhood stretching from Mollstra\u00dfe in the northeast to Spandauer Stra\u00dfe and the Rotes Rathaus in the southwest.",
                "coordinates":[
                    {
                        "lat":52.52166748,
                        "lon":13.41333294,
                        "primary":"",
                        "type":"landmark",
                        "dim":"1000",
                        "globe":"earth"
                    }
                ],
                "thumbnail":{
                    "source":"https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Alexanderplatz_by_the_night_-_ProtoplasmaKid.webm/400px--Alexanderplatz_by_the_night_-_ProtoplasmaKid.webm.jpg",
                    "width":400,
                    "height":225
                }
            },
            ...
        },
    }
    

    【讨论】:

    • 非常感谢。这正是我想做的。非常感谢。
    • @Termininja 我如何请求下一个结果页面?
    • @Termininja 我的意思是如何获得接下来的 20 个最近的地方。您的请求响应没有 continue 参数
    • @allo86 我已经回答过了 - 你不能有 more than 20 results
    猜你喜欢
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2014-08-17
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多