Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Python access to the Wikidata SPARQL endpoint #1

Open
baskaufs opened this issue Apr 26, 2023 · 0 comments
Open

Python access to the Wikidata SPARQL endpoint #1

baskaufs opened this issue Apr 26, 2023 · 0 comments

Comments

@baskaufs
Copy link

In your fantastic Scholarly API guide, you provide code for accessing the Wikidata Query Service (WDQS) using Mathematica, but not Python. I do quite a bit of Python programming with Wikidata and a while back I created some Python code to query SPARQL endpoints and the Wikidata Query Service in particular. The code is freely available here:

https://github.com/HeardLibrary/digital-scholarship/blob/master/code/wikidata/sparqler.py

and there's a blog post about it here.

There are some considerations that I discuss in the blog post, such as the User-Agent requirement. But the object can pretty much be copied and pasted into code and then just run with three lines of code:

user_agent = 'TestAgent/0.1 (mailto:[email protected])'
wdqs = Sparqler(useragent=user_agent)
data = wdqs.query(query_string)

once you've created the SPARQL query string and assigned it to query_string.

For Wikidata, only lines 22 through 162 (which define the query method) are actually needed since you can't write to the WDQS, so for use with Wikidata, one would not need to include the code for the entire object.

One issue with using the WDQS is that you need to know how to construct SPARQL queries. But there's quite a bit of information about that online and the Wikidata community is very helpful when it comes to assisting newcomers with learning.

Anyway, if this is helpful, you're welcome to use it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant