最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use Wikidata API to get ISO 3166-1 country codes? - Stack Overflow

programmeradmin3浏览0评论

I have been looking for examples online but can't find any for getting ISO 3166-1 country codes using the Wikidata API.

It's mentioned here that one can do or , but I don't really know the difference. Also, I'm used to using APIs that return JSON, which it seems like neither of these two endpoints are. Is there a way to get JSON?

Overall, just wondering how to do this properly since examples are few and far between (or none at all).

I have been looking for examples online but can't find any for getting ISO 3166-1 country codes using the Wikidata API.

It's mentioned here that one can do https://query.wikidata/sparql or https://query.wikidata/bigdata/ldf, but I don't really know the difference. Also, I'm used to using APIs that return JSON, which it seems like neither of these two endpoints are. Is there a way to get JSON?

Overall, just wondering how to do this properly since examples are few and far between (or none at all).

Share Improve this question asked Jul 5, 2020 at 5:50 RajRaj 1,66521 silver badges32 bronze badges 1
  • Would be nice if the person that downvoted could state what's wrong with the question so I can modify it accordingly. – Raj Commented Jul 5, 2020 at 14:33
Add a ment  | 

2 Answers 2

Reset to default 3

Use this SPARQL query. It get the list of countries that has ISO 36 code value. After entering the previous URL, You can click Download button to download the response to JSON, CSV...etc.

#Find ISO 3166-1 alpha-2 country codes
SELECT ?country ?countryLabel ?code
WHERE
{
    ?country wdt:P297 ?code .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}

Wikidata's API is not meant to be used for that kind of queries on the fly. You would need to get all items that have the property P297 (ISO 3166) which will give you the list of countries and then fetch each country with all it's claims just to get the ISO 3166 value.

Instead, why not have a copy of the codes on your server, or even fetch it from a clean source meant for that, like this one: https://restcountries.eu/rest/v2/all?fields=name;alpha2Code

发布评论

评论列表(0)

  1. 暂无评论