如何通过API从Wikidata项目获取图像URL属性? [英] How to get image URL property from Wikidata item by API?
问题描述
我制作了一个使用JSON Google图片搜索API提供图片的Android应用,但我注意到Google已停止支持它。我还发现维基数据有时会在某些项目上提供图像属性,但我似乎无法使用维基数据API获取图像的URL位置。
I've made an android app that uses the JSON Google image search API to provide images but I have noticed that Google have stopped supporting it. I have also discovered that Wikidata sometimes provides a image property on some items, however I can't seem to get the URL location of the image using the Wikidata API.
是有没有办法从维基数据中的项目获取图像URL属性?
Is there any way to get the image URL property from items in Wikidata?
推荐答案
如果有一些维基数据项目(ID: Qxxx )有图片(P18)属性,您可以通过 MediaWiki API :
If some Wikidata item (with ID: Qxxx) has image (P18) property, you can access it by MediaWiki API:
https://www.wikidata.org/w/api.php?action=wbgetclaims&entity=Qxxx&property=P18
回复将包括:
"claims": {
"P18": [{ "mainsnak": { "datavalue": { "value": "img_name.ext" }}}]
}
其中 img_name.ext 是t他要查找的图像的名称(所有空格都必须替换为 _
)。
where img_name.ext is the name of the image you are looking for (all whitespaces have to be replaced with _
).
最终图片网址为: https://upload.wikimedia.org/wikipedia/commons/a/ab/img_name.ext
,其中 a 和 b 是 img_name的 md5sum 的第一个和第二个字符.ext 。
The final image URL will be: https://upload.wikimedia.org/wikipedia/commons/a/ab/img_name.ext
, where a and b are the first and the second chars of md5sum of the img_name.ext.
例如,对于项目 jaguar(Q35694) API将返回图像名称 Junior-Jaguar-Belize-Zoo.jpg ,其中有md5sum: 21 3b31ec141dafebe457e49bcd7f9329 ,所以a = 2 和b = 1 ,或最终图片网址为: https://upload.wikimedia.org/wikipedia/commons/2/21/Junior-Jaguar-Belize-Zoo.jpg
For example, for item jaguar (Q35694) the API will returns image name "Junior-Jaguar-Belize-Zoo.jpg", which has md5sum: 213b31ec141dafebe457e49bcd7f9329, so a=2 and b=1, or the final image URL will be: https://upload.wikimedia.org/wikipedia/commons/2/21/Junior-Jaguar-Belize-Zoo.jpg
这篇关于如何通过API从Wikidata项目获取图像URL属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!