Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wikidata_sparqlwrapper-2024.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

wikidata_sparqlwrapper-2024.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file wikidata_sparqlwrapper-2024.1.tar.gz.

File metadata

File hashes

Hashes for wikidata_sparqlwrapper-2024.1.tar.gz
Algorithm Hash digest
SHA256 714c2625d588ea8ae04e6bd397ec896c69134ce6eb615b2643a2ec696a4c5846
MD5 08026d6aefa244d113139503083ae45c
BLAKE2b-256 f2bf111e5770bfd50696f4018f72ee565fee118ad89815063575c96635436634

See more details on using hashes here.

File details

Details for the file wikidata_sparqlwrapper-2024.1-py3-none-any.whl.

File metadata

File hashes

Hashes for wikidata_sparqlwrapper-2024.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7dff20d350906512881a22a9ab6dc8b9539fe9d339e4009be960198916e8de46
MD5 4ba7ff22ebecbf1fc9be2f742b51f64d
BLAKE2b-256 8bb66a4aa132657610f75c9a1759432790b262736d67168ac093e7209e299eb8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page