Wrapper around that Wikidata SPARQL endpoint that respects the Wikidata usage policy.
Project description
title: README author: Jan-Michael Rye
Synopsis
A light wrapper around SPARQLWrapper for Wikidata's SPARQL query service. The wrapper attempts to follow Wikidata's usage policy regarding user-agent strings and query frequency.
The wrapper also wraps expected exceptions in a custom exception class to facilitate error handling.
Links
GitLab
Usage
from wikidata_sparqlwrapper import (
WikidataSPARQLWrapper,
WikidataSPARQLWrapperError,
construct_user_agent_string,
)
# Construct a user-agent string that uniquely identifies your code and which
# includes a contact email address.
agent = construct_user_agent_string(name="MyApp", email="my_email@example.com")
# Instantiate the Wikidata SPARQL wrapper.
wrapper = WikidataSPARQLWrapper(user_agent=agent)
# Construct a SPARQL query.
query = """\
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31 wd:Q146.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
"""
# Get the SPARQLWrapper query object directly. The format will be set to JSON.
response = wrapper.query(query)
# Get the bindings directly.
bindings = wrapper.query_bindings(query)
# Do something with the bindings.
for binding in bindings:
print(binding["itemLabel"]["value"])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wikidata_sparqlwrapper-2024.1.tar.gz
.
File metadata
- Download URL: wikidata_sparqlwrapper-2024.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 714c2625d588ea8ae04e6bd397ec896c69134ce6eb615b2643a2ec696a4c5846 |
|
MD5 | 08026d6aefa244d113139503083ae45c |
|
BLAKE2b-256 | f2bf111e5770bfd50696f4018f72ee565fee118ad89815063575c96635436634 |
File details
Details for the file wikidata_sparqlwrapper-2024.1-py3-none-any.whl
.
File metadata
- Download URL: wikidata_sparqlwrapper-2024.1-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dff20d350906512881a22a9ab6dc8b9539fe9d339e4009be960198916e8de46 |
|
MD5 | 4ba7ff22ebecbf1fc9be2f742b51f64d |
|
BLAKE2b-256 | 8bb66a4aa132657610f75c9a1759432790b262736d67168ac093e7209e299eb8 |