【问题标题】:How use wikidata api to access to the statements如何使用wikidata api访问报表
【发布时间】:2020-10-08 19:25:42
【问题描述】:

我正在尝试从 Wikidata 获取信息。例如,要访问“cobalt-70”,我使用 API。

API_ENDPOINT = "https://www.wikidata.org/w/api.php"

query = "cobalt-70"

params = {
    'action': 'wbsearchentities',
    'format': 'json',
    'language': 'en',
    'search': query
}
r = requests.get(API_ENDPOINT, params = params)
print(r.json())

所以有一个“声明”可以访问这些语句。是否有最好的方法来检查语句中是否存在值?例如,“cobalt-70”在属性 P2114 中的值为 0.5。那么如何检查实体的语句中是否存在值?作为这个例子。

有没有办法访问它。谢谢!

【问题讨论】:

标签: json api parsing wikidata wikidata-api


【解决方案1】:

我尝试了一个解决方案,该解决方案包括在 json 对象内部进行搜索,作为此处提出的解决方案:https://stackoverflow.com/a/55549654/8374738。我希望它可以帮助。让我们给你一个想法。

import pprint

def search(d, search_pattern, prev_datapoint_path=''):
    output = []
    current_datapoint = d
    current_datapoint_path = prev_datapoint_path
    if type(current_datapoint) is dict:
        for dkey in current_datapoint:
            if search_pattern in str(dkey):
                c = current_datapoint_path
                c+="['"+dkey+"']"
                output.append(c)
            c = current_datapoint_path
            c+="['"+dkey+"']"
            for i in search(current_datapoint[dkey], search_pattern, c):
                output.append(i)
    elif type(current_datapoint) is list:
        for i in range(0, len(current_datapoint)):
            if search_pattern in str(i):
                c = current_datapoint_path
                c += "[" + str(i) + "]"
                output.append(i)
            c = current_datapoint_path
            c+="["+ str(i) +"]"
            for i in search(current_datapoint[i], search_pattern, c):
                output.append(i)
    elif search_pattern in str(current_datapoint):
        c = current_datapoint_path
        output.append(c)
    output = filter(None, output)
    return list(output)

你只需要使用:

pprint.pprint(search(res.json(),'0.5','res.json()'))

Output:

["res.json()['claims']['P2114'][0]['mainsnak']['datavalue']['value']['amount']"]

【讨论】:

    【解决方案2】:

    我不确定这正是您正在寻找的,但如果它足够接近,您可能可以根据需要对其进行修改:

    import requests
    import json
    url = 'https://www.wikidata.org/wiki/Special:EntityData/Q18844865.json'
    req = requests.get(url)
    targets = j_dat['entities']['Q18844865']['claims']['P2114']
    for target in targets:    
        values = target['mainsnak']['datavalue']['value'].items()
        for value in values:
            print(value[0],value[1])
    

    输出:

    amount +0.5
    unit http://www.wikidata.org/entity/Q11574
    upperBound +0.6799999999999999
    lowerBound +0.32
    amount +108.0
    unit http://www.wikidata.org/entity/Q723733
    upperBound +115.0
    lowerBound +101.0
    

    编辑: 要按值查找属性 id,请尝试:

    targets = j_dat['entities']['Q18844865']['claims'].items()
    for target in targets:   
        line = target[1][0]['mainsnak']['datavalue']['value']
        if isinstance(line,dict):
            for v in line.values():
                if v == "+0.5":
                    print('property: ',target[0])
    

    输出:

    property:  P2114
    

    【讨论】:

    • 非常感谢您的回答。这不正是我要找的。想象一下,你的值是 0.5,我想知道它是否存在于“cobalt-70”的语句中,并返回 0.5 的属性 id(“property”:“P2114”)。
    • @elitebook190 - 我想我明白你在寻找什么(希望如此)。见编辑。
    猜你喜欢
    • 1970-01-01
    • 2016-09-20
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2019-09-05
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多