(首先,我很抱歉,因爲這是一個公然的交叉帖子。我以爲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,沒有任何合理的默認限制。 –