(首先,我很抱歉,因为这是一个公然的交叉帖子。我以为opendata.SE会是这个地方,但它几乎没有任何意见,它似乎并不是一个非常活跃的网站,所以我想我应该在这里尝试它,因为它与编程有关。)为什么这个SPARQL查询缺少这么多结果?
我想要获取世界各大城市的名单:他们的名字,人口和位置。我发现什么看起来像一个很好的查询上Wikidata,稍微调整自己的内置查询一个例子:
SELECT DISTINCT ?cityLabel ?population ?gps WHERE {
?city (wdt:P31/wdt:P279*) wd:Q515.
?city wdt:P1082 ?population.
?city wdt:P625 ?gps.
FILTER (?population >= 500000) .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population)
结果,乍一看,似乎是不错的,但它缺少一吨的重要城市。例如,当我特别要求所有人口超过500,000的城市时,旧金山(人口800,000+)和西雅图(人口650,000+)不在列表中。
我的查询有什么问题吗?如果没有,那么Wikidata正在使用的数据肯定有问题。无论哪种方式,我怎样才能获得一个有效的数据集,我可以从Python脚本查询API? (我已经得到了脚本全部为此工作;我只是没有回来有效的数据。)
from SPARQLWrapper import SPARQLWrapper, JSON
from geopy.distance import great_circle
def parseCoords(gps):
base = gps[6:-1]
coords=base.split()
return (float(coords[1]), float(coords[0]))
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
sparql.setReturnFormat(JSON)
sparql.setQuery("""SELECT DISTINCT ?cityLabel ?population ?gps WHERE {
?city (wdt:P31/wdt:P279*) wd:Q515.
?city wdt:P1082 ?population.
?city wdt:P625 ?gps.
FILTER (?population >= 500000) .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population)""")
queryResults = sparql.query().convert()
cities = [(city["cityLabel"]["value"], int(city["population"]["value"]), parseCoords(city["gps"]["value"])) for city in queryResults["results"]["bindings"]]
print (cities)
你会得到多少结果?可能是端点有一个默认限制 - 例如在DBpedia上,您最多可以获得10000个条目,但您必须使用OFFSET + LIMIT又名。分页。 – AKSW
@AKSW 250,没有任何合理的默认限制。 –