How to use rdflib to query WikiData?

Viewed 464

I mean that I want to use rdflib to query WIkidata in my local computer, but rdflib.Graph() need to parse the namespace firstly.THerefore, How can I get the Wikidata NameSpace to use the rdflib local code?

1 Answers

I think the goal was:

from rdflib import Graph

g = Graph()
g.parse('wikidata-link')

or

g.load('wikidata-link')

I haven't spent much time on it, but here are my tryouts, just to kinda complete the question and maybe find an answer.

Some of the following possible versions have resulted in some kind of error ranging from, 'timeout', 'not well formed (invalid token)', Typeerrors, '.. not a valid NCName ...' up to missing plugin errors when getting 'text/html' or '.../json' back. I marked what worked and what didn't.

CODE SAMPLES I'VE TRIED

g.parse('https://www.wikidata.org/wiki/Special:EntityData/Q42.n3') #  WORKS
g.parse('https://www.wikidata.org/wiki/Special:EntityData/Q42.json') #  FAILS
g.parse('https://www.wikidata.org/wiki/Special:EntityData/Q42.ttl') #  WORKS
g.parse('https://www.wikidata.org/wiki/Special:EntityData/Q42.rdf') #  FAILS
g.parse('https://www.wikidata.org/wiki/Special:EntityData/Q64') #  FAILS
g.parse('https://www.wikidata.org/wiki/Q42') #  FAILS
g.load('https://www.wikidata.org/wiki/Special:EntityData/Q42.n3') #  FAILS
g.load('https://www.wikidata.org/wiki/Special:EntityData/Q42.json') #  FAILS
g.load('https://www.wikidata.org/wiki/Special:EntityData/Q42.ttl') #  FAILS
g.load('https://www.wikidata.org/wiki/Special:EntityData/Q42.rdf') #  FAILS
g.load('https://www.wikidata.org/wiki/Special:EntityData/Q42') #  FAILS
g.load('https://www.wikidata.org/wiki/Q42') #  FAILS

I tried these out based on Wikidata Access

VERSIONS USED

RDFLib 6.1.1 Python 3.10.1

Last additional thoughts

You could query wikidata via the endpoint and build your rdflib graph from there.

Related