Wikidata:Creating a bot

From Wikidata
Jump to: navigation, search

Other languages:
dansk • ‎Deutsch • ‎Zazaki • ‎English • ‎British English • ‎Esperanto • ‎español • ‎فارسی • ‎suomi • ‎français • ‎Frysk • ‎ગુજરાતી • ‎italiano • ‎日本語 • ‎Ripoarisch • ‎latviešu • ‎മലയാളം • ‎Nederlands • ‎occitan • ‎polski • ‎русский • ‎தமிழ் • ‎తెలుగు • ‎中文

This page explains how to create bots for Wikidata. Please, consider sharing your code, add new examples and any improvements you want.


To create bots you need:

  • Some coding skills (Python, Perl, PHP...)
  • A framework (one of the frameworks below) and some code to run to complete a task
  • A bot account (and approved)
  • A source code editor (Notepad++, Geany, vi, emacs)


In the next sections you learn how to install, configure and login using pywikibot. You only need to do these first three steps once. Also, there are some basic examples to learn the basics about bot programming.


For further details about pywikibot installation, see mw:Manual:Pywikibot/Installation.

To install pywikibot:


For further details about pywikibot configuration, see mw:Manual:Pywikibot/

You must add a line in the file with the bot username, family project and language. For Wikidata both family and language parameters are the same.

You can reduce the delay between edits adding: put_throttle = 1


After you configure the file, login as follows:


It will ask you for the bots password, type it and press enter. If you did it correctly, you should be logged in now.

Example 1: Get data[edit]

This example gets data for the page refering to Douglas Adams. Save the following source code in a file and execute it: python

item.get() connects to Wikidata and fetches the data. The output is:

{u'description': {u'en': u'English writer and humorist', u'zh': u'\u82f1\u56fd\u4f5c\u5bb6', u'ca': u'escriptor angl\xe8s', u'it': u'Scrittore inglese', u'zh-hk': u'\u82f1\u570b\u4f5c\u5bb6', u'pt-br': u'escritor e humorista ingl\xeas',...}}
['claims', 'labels', 'sitelinks', 'descriptions', 'aliases']

It prints the entire dictionary. Later a list with all the keys for the key-values pairs. Finally, you can see that the item about Douglas Adams is Q42.


The example above gets the ItemPage using the en wikipedia article. Alternatively, we can also get the ItemPage directly:

Example 2: Get interwiki links[edit]

After item.get(), for example the sitelinks can be accessed. These are links to all Wikipedias that have the article.

The output is:

{u'fiwiki': u'Douglas Adams', u'eowiki': u'Douglas Adams', u'dewiki': u'Douglas Adams', ...

With item.iterlinks(), an iterator over all these sitelinks is returned, where each article is given not as plain text as above but already as a Page object for further treatment (e.g., edit the text in the corresponding Wikipedia articles).

Example 4: Set a description[edit]

This example sets an English and a German description for the item about Douglas Adams.

Setting labels and aliases works accordingly.

Example 6: Set a sitelink[edit]

To set a sitelink, we can either create a corresponding dict corresponding to Example 4 or use Page objects:

Example 7: Set a statement[edit]

Statements are set using the Claim class. In the following, we set for Douglas Adams place of birth (P19): Cambridge (Q350).

For other datatypes, this works similar. In the following, we add claims with string ((OBSOLETE) quote (use P1683 ) (P387) and coordinate (coordinate location (P625)) datatypes (URL is the same as string):

Example 8: Add a qualifier[edit]

Qualifiers are also represented by the Claim class. In the following, we add the qualifier incertae sedis (P678) family (Q35409) to the Claim "claim".

Example 9: Add a source[edit]

Also, sources are represented by the Claim class. Unlike for qualifiers, a source may contain more than one Claim. In the following, we add stated in (P248) Integrated Taxonomic Information System (Q82575) with retrieved (P813) March 20, 2014 as source to the Claim "claim".

Example 10: Page generators[edit]


More examples[edit]

Some users share their source codes. Learn more in the next links:


Wikibase.NET is the api that replaces the now deprecated DotNetDataBot. They aren't compatible because Wikibase.NET does no longer need the DotNetWikiBot framework.

Download & Installation[edit]

The framework can be downloaded from GitHub here. Just follow the instructions on that page.

Known issues[edit]


Coming soon ...

DotNetDataBot (Deprecated)[edit]



After unpacking the package you can see a file called DotNetDataBot.dll and one called DotNetDataBot.xml. The xml document is only for documentation. To use it you have to create a new refer in your project. Then you can write using DotNetDataBot; to import the framework.


To login you have to create a new Site object with the url of the wiki, your bot's username and it's password.

Example 1: Get id using wiki page[edit]

You can access the id of an item by searching for using the site and the title of the connected page.

Example 2: Get interwiki links[edit]

You can get the interwiki links of an item by loading the content and accessing the links field of the object.

Example 3: Set a description[edit]

To set a description, you must call the setDescription function.

Example 4: Set a label[edit]

It works the same way for setting a label. Just call setLabel.

Example 5: Get interwiki links for 100 pages[edit]

This feature is not supported. Just iterate over the list.

Wikibase api for PHP[edit]

This is an api client for Wikibase written in PHP. It can be downloaded from here.

Example 1: Basic example[edit]

Take a look at the source comments to understand how it works.

Example 2: Creating claims[edit]

Take a look at the source comments to understand how it works.


In this section you will learn how to query directly to API. API queries are needed if you want to develop your own framework or to code an unsupported function for a framework. A documentation for the Wikibase api can be found at You can also have a look at Special:ApiSandbox.

For these API examples you don't need to install any additional software, just the language interpreter (Python, Perl, etc), and support frameworks to perform Http requests. Also you have to parse the result, so a JSON or XML parser is needed.

Example 1: Get Q number[edit]

This example gets the item Q number that includes the sitelink for English Wikipedia article about Andromeda Galaxy.


The output is:



Framework for Wikidata and Wikipedia. Read and write on Wikidata and other Wikimedia project and have a useful list generator to generate list of Wikipedia page and Wikidata entity. Can read also JSON dump of WIkidata.


Bot to read and edit Wikidata and Wikipedia.

  • License: CC0 1.0
  • Language C#
  • Can read and write entities with all datatype on Wikidata
  • Can read and write pages on all Wiki project
  • Can read parameter from template on wiki pages
  • Can read JSON dump
  • Can create lists using:
  • Tested with Visual Studio Express 2013 for Windows Desktop.
    • Is necessary to have Newtonsoft.Json. You can install it with NuGet inside Visual Studio
    • Is necessary to add manually a reference to System.Web for "HttpUtility.UrlEncode"


The framework can be downloaded from GitHub here.


Example 1[edit]

Update en label for all item with instance of (P31): short film (Q24862) and that have director (P57) and that have publication (P577) in 1908. (Use of Wikidata query)

See also[edit]

External links[edit]