【问题标题】:How to query the wikipedia api for a City in the United states如何查询美国城市的维基百科 api
【发布时间】:2016-10-16 01:59:12
【问题描述】:

给定一串文本,例如:San Francisco, California 我正在尝试使用 jquery 获取请求来取回该城市的维基百科页面。我找到了Wikipedia web service API,但我无法让它工作。我的目标是只获得概述部分。

我尝试将浏览器指向以下网址:

https://en.wikipedia.org/w/api.php?action=query&titles=San%20Francisco,%20California&prop=revisions&rvprop=content&format=jsonfm 但它返回为:

This is the HTML representation of the JSON format. HTML is good for debugging, but is unsuitable for application use.
Specify the format parameter to change the output format. To see the non-HTML representation of the JSON format, set format=json.
See the complete documentation, or the API help for more information.
{
    "batchcomplete": "",
    "query": {
        "pages": {
            "19946864": {
                "pageid": 19946864,
                "ns": 0,
                "title": "San Francisco, California",
                "revisions": [
                    {
                        "contentformat": "text/x-wiki",
                        "contentmodel": "wikitext",
                        "*": "#REDIRECT [[San Francisco]]\n{{R from city and state}}"
                    }
                ]
            }
        }
    }
}

如果我添加&redirects,那么我会得到预期的响应:

https://en.wikipedia.org/w/api.php?action=query&titles=San%20Francisco,%20California&prop=revisions&rvprop=content&format=jsonfm&redirects

This is the HTML representation of the JSON format. HTML is good for debugging, but is unsuitable for application use.
Specify the format parameter to change the output format. To see the non-HTML representation of the JSON format, set format=json.
See the complete documentation, or the API help for more information.
{
    "batchcomplete": "",
    "query": {
        "redirects": [
            {
                "from": "San Francisco, California",
                "to": "San Francisco"
            }
        ],
        "pages": {
            "49728": {
                "pageid": 49728,
                "ns": 0,
                "title": "San Francisco",
                "revisions": [
                    {
                        "contentformat": "text/x-wiki",
                        "contentmodel": "wikitext",
                        "*": "{{About|the city and county in California}}\n{{pp-move-indef}}\n\n{{Use mdy dates |date = July 2016}}\n\n{{Infobox settlement\n<!--See the table at Template:Infobox settlement for all fields and descriptions of their usage-->\n| name = San Francisco, California\n| settlement_type = [[Consolidated city-county]]\n| official_name = City and County of San Francisco\n| image_skyline = Golden Gate Bridge, SF (cropped).jpg\n| image_caption = San Francisco and the [[Golden Gate Bridge]] from [[Marin Headlands]]\n| image_flag = Flag of San Francisco.svg\n| flag_size = 100px\n| image_seal = Seal of San Francisco.png\n| seal_size = 100px\n| nickname = ''The City''; ''The City by the Bay''; ''Fog City''; ''San Fran'';{{efn|This name, like Frisco, has often been discouraged amongst Bay Area natives.}} ''Frisco'' (''locally disparaged'');<ref name=\"Frisco okay\" /><ref name=\"Don't Call It Frisco\" /><ref name=\"Frisco\" /><ref name=\"Friscophobia\" /> ''The City that Knows How'' (''past'');<ref name=\"The City that Knows How\" /> ''[[Baghdad]] by the Bay'' (''past'');<ref name=\"Baghdad by the Bay\" /> ''The Paris of the West''<ref name=\"The Paris of the West\"/>\n| motto = ''Oro en Paz, Fierro en Guerra'' (Spanish)<br />(English: \"Gold in Peace, Iron in War\")\n| image_map = California county map (San Francisco County enlarged).svg\n| ma

如果我尝试另一个城市,州查询它可以工作:

https://en.wikipedia.org/w/api.php?action=query&amp;titles=Martinez,%20California&amp;prop=revisions&amp;rvprop=content&amp;format=jsonfm&amp;redirects

本页对应Marintez, California

This is the HTML representation of the JSON format. HTML is good for debugging, but is unsuitable for application use.
Specify the format parameter to change the output format. To see the non-HTML representation of the JSON format, set format=json.
See the complete documentation, or the API help for more information.
{
    "batchcomplete": "",
    "query": {
        "pages": {
            "107407": {
                "pageid": 107407,
                "ns": 0,
                "title": "Martinez, California",
                "revisions": [
                    {
                        "contentformat": "text/x-wiki",
                        "contentmodel": "wikitext",
                        "*": "{{Use mdy dates|date=March 2016}}\n{{Use American English|date=March 2016}}\n\n{{Infobox settlement\n<!-- See the table at Template:Infobox settlement for all fields and descriptions of their usage. -->\n| official_name              = City of Martinez<ref>{{cite web |url=http://www.cityofmartinez.org/ |title=Homepage |publisher=City of Martinez |accessdate=November 20, 2014}}</ref>\n| settlement_type            = [[City (California)|City]]\n<!-- Images and maps ---- -->\n| image_skyline              = Aerial view of Martinez, California.jpg\n| image_caption              = Aerial view of Martinez\n| image_seal                 =\n| image_map                  = Contra Costa County California Incorporated and Unincorporated areas Martinez Highlighted.svg\n| mapsize                    = 250px\n| map_caption                = Location in [[Contra Costa County, California|Contra Costa County]] and the state of [[California]]\n<!-- Location ----------- -->\n| pushpin_map                = USA\n| pushpin_map_caption = Location in the United States\n| latd = 38 |latm = 01 |lats = 10 |latNS = N\n| longd = 122 |longm = 08 |longs = 03 |longEW = W\n| coordinates_display        = inline,title\n| coordinates_region         = US-CA\n| subdivision_type           = [[List of sovereign states|Country]]\n| subdivision_name           = {{USA}}\n| subdivision_type1          = [[U.S. state|State]]\n| subdivision_name1          = {{flag|California}}\n| subdivision_type2          = [[List of counties in California|County]]\n| subdivision_name2          = [[Contra Costa County, California|Contra Costa]]\n<!-- History ------------ -->\n| established_title          = [[Municipal corporation|Incorporated]]\n| established_date           = April 1, 1876<ref>{{cite web\n | url = http://www.calafco.org/docs/Cities_by_incorp_date.doc\n | title = California Cities by Incorporation Date\n | format = Word\n | publisher = California Association of [[Local Agency Formation Commission]]s\n | accessdate = March 27, 2013}}</ref>\n<!-- Government --------- -->\n| government_type            =\n| leader_title               = [[Mayor]]\n| leader_name                = Rob Schroder<ref>{{cite web\n |url=http://www.cityofmartinez.o

但现在的问题是我将如何进入概述并将其刮掉。似乎应该可以只查询概述或摘要。

【问题讨论】:

  • 你检查答案了吗?

标签: javascript api search wikipedia


【解决方案1】:

要获得json 的响应,而不是html,您应该使用

format=json

而不是

format=jsonfm

jsonfm 返回一个“漂亮打印”的 HTML 结果,有利于调试。

这是您应该使用的请求:

https://en.wikipedia.org/w/api.php?action=query&titles=San%20Francisco,%20California&prop=revisions&rvprop=content&format=json

您还可以将format=xmlformat=php 用于xml 输出或php serialized 值。

【讨论】:

  • 它需要添加 &amp;redirects 才能真正获得 sf 页面,但是使用 &amp;titles=San%20Francisco,%20California 方法搜索有问题,因为它总是能成功到达该城市的 wiki 页面,即使有重定向.我现在将编辑问题
【解决方案2】:

仅获取概览部分的关键是使用redirectsextracts,如下所示:

curl "https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&explaintext&format=json&redirects&titles=San_Francisco,_CA"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    相关资源
    最近更新 更多