在页面上,数据清楚地以可读格式显示,但是当您尝试通过API提取数据时,会得到以下信息:https : //www.wikidata.org/w/api.php?action=wbgetentities&ids=Q24871
这是其中的一部分:
"P272": [ { "id": "q24871$4721C959-0FCF-49D4-9265-E4FAC217CB6E", "mainsnak": { "snaktype": "value", "property": "P272", "datatype": "wikibase-item", "datavalue": { "value": { "entity-type": "item", "numeric-id": 775450 }, "type": "wikibase-entityid" } }, "type": "statement", "rank": "normal" }, { "id": "q24871$31777445-1068-4C38-9B4B-96362577C442", "mainsnak": { "snaktype": "value", "property": "P272", "datatype": "wikibase-item", "datavalue": { "value": { "entity-type": "item", "numeric-id": 3041294 }, "type": "wikibase-entityid" } }, "type": "statement", "rank": "normal" }, { "id": "q24871$08009F7A-8E54-48C3-92D9-75DEF4CF3E8D", "mainsnak": { "snaktype": "value", "property": "P272", "datatype": "wikibase-item", "datavalue": { "value": { "entity-type": "item", "numeric-id": 646968 }, "type": "wikibase-entityid" } }, "type": "statement", "rank": "normal" }, { "id": "q24871$CA53B5EB-1041-4701-A36E-7C348FAC984E", "mainsnak": { "snaktype": "value", "property": "P272", "datatype": "wikibase-item", "datavalue": { "value": { "entity-type": "item", "numeric-id": 434841 }, "type": "wikibase-entityid" } }, "type": "statement", "rank": "normal", "references": [ { "hash": "50f57a3dbac4708ce4ae4a827c0afac7fcdb4a5c", "snaks": { "P143": [ { "snaktype": "value", "property": "P143", "datatype": "wikibase-item", "datavalue": { "value": { "entity-type": "item", "numeric-id": 11920 }, "type": "wikibase-entityid" } } ] }, "snaks-order": [ "P143" ] } ] } ],
问题是我不确定如何将类似的部分转换为可读的文本。我知道API使用唯一的ID调用了类及其属性之间的链接,但我仍然陷入困境。
目前这实际上可行吗,还是我叫错了树?
您应该numeric-id在每个语句中寻找s,并添加一个Q线索来恢复您的wikidata id,这应该导致['Q775450', 'Q3041294', 'Q646968', 'Q434841', 'Q11920']
numeric-id
Q
['Q775450', 'Q3041294', 'Q646968', 'Q434841', 'Q11920']
[ 更新 :您现在可以直接从访问Q id mainsnak.datavalue.value.id,而不必从numeric-id] 进行构建
mainsnak.datavalue.value.id
可以使用wikibase-sdk(我开发的JS lib)wbk.simplify.claims功能来完成
wbk.simplify.claims
取得这些ID后,您只需要使用wbgetentitiesAPI 请求实体标签: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q775450|Q3041294|Q646968|Q434841|Q11920&format=json&props=labels
wbgetentities
https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q775450|Q3041294|Q646968|Q434841|Q11920&format=json&props=labels
您甚至可以使用languages参数来获得某些语言的结果:https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q775450|Q3041294|Q646968|Q434841|Q11920&format=json&props=labels&languages=en|de|fr
languages
https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q775450|Q3041294|Q646968|Q434841|Q11920&format=json&props=labels&languages=en|de|fr