Get all Wikidata items that are an instance of a given item

心不动则不痛 提交于 2019-12-30 06:27:48

问题


Wikidata has an item called smartphone model.
I want to get all instances of it.

QUESTION: How to get the identifiers of the instances programmatically, using the live server?

Preferably not including false positives that show up in WhatLinksHere but are in the "Wikidata:" namespace rather than the main namespace.


回答1:


Your question specifies the "Mediawiki API" but this is not possible.

Wikidata has a SPARQL query service at https://query.wikidata.org

The query that you want is:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT ?item
WHERE {
    ?item wdt:P31/wdt:P279* wd:Q19723451
}

This will list the Items that are an 'instance of'(P31) 'smartphone model'(Q19723451) or an 'instance of' a 'subclass of'(P279) 'smartphone model'(Q19723451).




回答2:


It is not possible yet. (Here is the task if you want to follow the development.)

The best you could to today in Mediawiki API is to use action=query and props=linkshere and filter on namespace like this:

http://www.wikidata.org/w/api.php?action=query&prop=linkshere&format=json&lhprop=title&lhnamespace=0&lhlimit=500&titles=Q19723451

However, there is an external query tool, Wikidata query, that would do exactly what you are asking for with this query:

http://wdq.wmflabs.org/api?q=CLAIM[31:19723451]

Just add Q in front of the numbers and you have the item id.



来源:https://stackoverflow.com/questions/29886388/get-all-wikidata-items-that-are-an-instance-of-a-given-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!