如何从维基百科中获取城市的缺失信息

How to get missing information for a city from wikipedia

为了一个项目,我下载了一些维基百科城市页面,比如我家乡的页面 Markdorf。正如您在维基百科页面上看到的那样,城市面积显示在 "Fläche" 旁边,人口显示在 "Einwohner" 旁边。

如何从 api 中获取这些数据?当我下载 json version of the Wikipedia page of Markdorf 时,响应当然包含 "Fläche" 和 "Einwohner" 但旁边没有值。我希望得到像 "Landkreis" 这样的值,在维基百科页面的 json 版本中返回为 "key value pair":Landkreis = Bodenseekreis.

Fläche 被列为 Fläche<ref name="Daten & Fakten">[http://markdorf.de/index.php?id=351 ''Daten & Fakten''] auf der Internetseite der Stadt Markdorf, abgerufen am 29.&nbsp;Mai 2015.</ref> | 没有任何数据。引用的网站 http://markdorf.de/index.php?id=351 当然包含信息,但无法解析。

那么:如何使用 Wikipedia api 访问 FlächeEinwohner 等信息? Bevölkerungsdichte 也根本不返回。

Tgr is right, you should use a structured data source instead of trying to parse the wikitext directly. You could use the wikidata query service 构建一个 SPARQL 查询,returns 基于城镇名称的面积和人口。该查询可能如下所示:

SELECT ?town ?townLabel ?area ?population WHERE {
  ?town ?label "Markdorf"@de.     # find the item labeled "Markdorf" in German
  ?town wdt:P2046 ?area.          # get the area(wdt:P2046) of that item
  ?town wdt:P1082 ?population.    # get the population(wdt:P1082) of that item
  SERVICE wikibase:label { bd:serviceParam wikibase:language "
[AUTO_LANGUAGE],de". }
}

Link to the query above

可以通过 Wikidata JSON endpoint 访问该查询的结果(查询只是编码为 URL 中的 query 参数)。