从Wikidata获取可读的结果 [英] Getting readable results from Wikidata

查看:142
本文介绍了从Wikidata获取可读的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我试图从Wikidata中获取有关电影的信息,以这部电影为例: https: //www.wikidata.org/wiki/Q24871

Ok so I'm trying to get information from Wikidata about movies, take this movie for example: https://www.wikidata.org/wiki/Q24871

在页面上,数据清楚地以可读格式显示,但是当您尝试通过API提取数据时,会得到以下信息:

On the page the data is clearly displayed in a readable format, however when you trying to extract it via the API you get this: 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来调用类及其属性之间的链接,但我仍然被卡住.

The problem is I'm not sure how to convert sections like that into readable text. I get the API is calling a link between a class and its properties using unique IDs but I'm still stuck.

这实际上是否可能,或者我在吠错树了吗?

Is this actually possible at present or am I barking up the wrong tree?

推荐答案

您需要查找的是每个语句中的numeric-id,并添加前导Q以恢复您的wikidata id,这将导致['Q775450', 'Q3041294', 'Q646968', 'Q434841', 'Q11920']

What you should be looking for are the numeric-ids in each statements and add a leading Q to recover your wikidata ids, which should result to ['Q775450', 'Q3041294', 'Q646968', 'Q434841', 'Q11920']

[更新:您现在可以直接在mainsnak.datavalue.value.id上访问Q id,而不必从numeric-id]构建它.

[update: you can now directly access the Q id at mainsnak.datavalue.value.id, instead of having to build it from the numeric-id]

可以使用 wikidata-sdk (我开发的JS库)完成 simplifyClaims函数

This can be done using wikidata-sdk (a JS lib I developed) simplifyClaims function

获得这些ID后,您只需要使用wbgetentities API请求实体标签: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q775450|Q3041294|Q646968|Q434841|Q11920&format=json&props=labels

Once you got those ids, you just need to request entities labels using the wbgetentities API: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q775450|Q3041294|Q646968|Q434841|Q11920&format=json&props=labels

您甚至可以使用languages参数获得某些语言的结果:

you can even get results for only some languages, using the languages parameter: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q775450|Q3041294|Q646968|Q434841|Q11920&format=json&props=labels&languages=en|de|fr

这篇关于从Wikidata获取可读的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆