Wikidata and Wikidata API - bounswe/bounswe2024group4 GitHub Wiki

What is Wikidata

Wikidata is a free and open knowledge base which is maintained by Wikidata editors and also automatic bots. Wikidata assist Wikipedia and other Wikimedia services with easily maintainable and nicely structured information.

Properties of Wikidata

Multilingual: Data in Wikidata is held in many languages, also adding information in new languages is fairly easy.

Free and Open: Anyone in the world can reach, copy, use and distribute (including commercial usage) data from Wikidata without any charges.

Structured Data: Wikidata holds its data in a very structured manner. Any data about one subject is held as properties with its references (if any). Also, data is hold with their resources so it is easier for users to make deeper research on topics.

Comprehensive Data: Wikidata tries to hold as much data as possible under a subject and it is successful at doing this.

How Does Wikidata Operate

Data is maintained by human editors or automatic bots which update statistics. Data is Wikidata mainly consists of items with unique identifiers which start with the letter Q followed by a number. This item contains a description and properties. Description is the main text which basically explains what the item is. Properties are specifications of the item. One item can consists of so many properties which can be linked to different data from Wikidata. For example, in the Wikidata page for Lebron James, member of sports team is one of our properties and it contains links to all pages James has played for in the NBA and it also contains some specifications as start time and end time.

Wikidata Query Service

There are different ways to make use of Wikidata such as built-in tools and external tools. One of the outstanding options is using the Wikidata Query Service. WQS takes SPARQL queries from users and returns corresponding data from Wikidata database. Using WQS users can reach multiple pages which correspond to any topic they would like. (ex. You can easily write a query that returns the item names and identifiers of the NBA teams) WQS also offers a Query Creator service, so that users who do not know how to write queries can use the website too.

What is Wikidata API?

Wikidata API is an API provided by Wikidata for users in order to make them able to use information from Wikidata databases in their own programs.

How to Access Wikidata API

It can be reached using the Wikidata API which may be reached with these libraries:

  • wikidata' and 'wikidataintegrator for Python
  • wikidata-sdk for Javascript
  • Wikidata-sdk-java for Java Also, the API can be reached with HTTP requests using these libraries:
  • requests for Python
  • axios for Javascript
  • OkHttp for Java

How to Use Wikidata API in Code?

In Python language, Wikidata API can be used as such:

# retrive all information about the item Q1234 in Wikidata 

url = 'https://www.wikidata.org/w/api.php'. # Url to Wikidata API
id = Q1234
response = requests.get(url, params = {'action': 'wbgetentities', 'format': 'json', 'ids': id, 'language': 'en'}). # Send request to get info of Q1234
data = response.json()  # Read the JSON response
print(data)

Also, we can send SPARQL queries using the API such below:

# get the results of the query which will replace YOUR_QUERY

query = YOUR_QUERY  # Put your query here as a string
url = "https://query.wikidata.org/sparql"  # Url to Wikidata API's query service
response = requests.get(url, params={'format': 'json', 'query': query})  # Send request to API with the query
data = response.json()
print(data)

Resources

Wikidata Introduction Page

Wikidata Main Page