首页 > 解决方案 > JSON decoding error when querying Wikidata

问题描述

I get a decoding error trying to decode my json data obtained from wikidata. I've read it may be because I'm trying to decode bytes instead of UTF-8 but I've tried to decode into UTF-8 and I can't seem to find the way to do so... Here's my method code (the parameter is a string and the return a boolean):

def es_enfermedad(candidato):
    url = 'https://query.wikidata.org/sparql'
    query = """
            SELECT ?item WHERE {
              ?item rdfs:label ?nombre.
              ?item wdt:P31 ?tipo.
              VALUES ?tipo {wd:Q12135}
              FILTER(LCASE(?nombre) = "%s"@en)
            }
            """ % (candidato)
    r = requests.get(url, params = {'format': 'json', 'query': query})
    data = r.json()
    return len(data['results']['bindings']) > 0

标签: pythonencodingwikidata

解决方案


推荐阅读