WikidSpark
WikidSpark is a python module with the aim of providing easy access to the WikiData SPARQL database. The aim is to provide a friendly method which allows those unfamiliar with SPARQL and databases in general to still access the information.
Querying WikiData
There are currently two methods for retrieving data from the WikiData site.
Direct Query
The first is query the site directly by passing the relevant id (QXYZ) to the query service. Two methods can be used to do this: get_by_id or get_by_name. Both of these methods come with two optional arguments for filtering the results:
from wikidspark.query import get_by_id
# Search for the Douglas Adams WikiData entry
dga_id = 'Q42'
dga_query_full = get_by_id(dga_id)
dga_query_language = get_by_id(dga_id, language='french')
the retrieve-by-name method currently makes use of the pre-requisite wikipedia model to find items relevant to the search, making it limited in terms of only displaying those entries which have an article attached to them. The function returns the first match it finds:
from wikidspark.query import get_by_name
dga_query_full = get_by_name('Douglas Adams')
dga_query_filtered = get_by_name('Douglas Adams', language='english', keys=['labels', 'descriptions'])
for a wider search it is recommended to use the find_item function to firstly return the relevant id, then use the get_by_id function. The find_item function has additional arguments to fetch only the first result (the default) or a list of matches, and to specify the wikipedia language:
dga_id = find_id('Douglas Adams')
london_ids = find_id('London', get_first=False, language='english')
Built Query
This method uses a class QueryBuilder to construct a SPARQL query to be sent to the WikiData query service. The query is built in stages, firstly by defining an instance of the builder (with the optional argument language),then adding conditions before finally fetching the results as a QItem object. This object allows you to view the data as XML, JSON or a Pandas Dataframe. The data returned currently has the additional properties Label, Description and AltLabel which are turned off by default but can be activated.
# Find first 100 books
from wikidspark.query import QueryBuilder
my_query = QueryBuilder("english")
my_query.instance_of('book')
my_query.Description = True
my_query.Label = True
result = my_query.get(limit=100)
json_res = result.json
xml_res = result.xml
df_res = result.dataframe
You can also search using a property ID and string value:
# Find Richmond station via CRS code 'RMD'
from wikidspark.query import QueryBuilder
my_query = QueryBuilder("english")
my_query.Description = True
my_query.Label = True
my_query.property_equals("P4755", "RMD")
df_res = result.dataframe
The functions available to the QueryBuilder class (e.g. instance_of) are based on an extensive list of WikiData properties, the full list can be fetched as a dataframe:
QueryBuilder().list_properties()
Metadata
Release files for wikidspark 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wikidspark-0.3.0.tar.gz | 145.8 kB | With provenance | Details for wikidspark-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wikidspark-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for wikidspark-0.3.0-py3-none-any.whl |
Total release size: 296.1 kB
Release files / wikidspark-0.3.0.tar.gz
| Download URL | wikidspark-0.3.0.tar.gz |
|---|---|
| Size | 145.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b10a111369ef478f3a946254fcf755e843c866756ae34747244946f9f2992a3
|
|
BLAKE2b-256 checksum How to use checksums |
c0be94bf1fa941331587058fae913c8df137536e610571707f2c6068937ada25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 24, 2025.
Transparency logRelease files / wikidspark-0.3.0-py3-none-any.whl
| Download URL | wikidspark-0.3.0-py3-none-any.whl |
|---|---|
| Size | 150.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dd17d8fe4c344a2497312bae3c776a52095e97e43807fa87988cc88d53842b3a
|
|
BLAKE2b-256 checksum How to use checksums |
fc29492e96ac945521c807fe43167894f7018ddb606b43ad741795a7e07a0f6b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 24, 2025.
Transparency logSecurity for wikidspark 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for wikidspark-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 24, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.