Wikidata:Pywikibot - Python 3 Tutorial

This is an update of the tutorial Wikidata:Creating a bot. But it is also an extension into a fully independent short-course that will show you how to work with Python 3, Wikidata and Pywikibot to handle large datasets.

Most of the text is rewritten to be more expressive and accommodating to people with little or no programming experience. The reader's interest in Wikidata might just be the reason that they want to learn programming.

In order to follow the course, you should have a few days or ideally a few weeks of Python 3 experience. The goal is to reach a level where you can run a bot to gather and edit information and transfer the information between other tools of your workflow.

Using Python and Pywikibot will allow the whole tutorial to be in one programming language. A language that is not only easy to learn, but also happens to be one of the most-used languages in the data-sciences. Focusing on Python 3 (emphasis on the version) will allow the tutorial a much more native approach to the multi-lingual nature of Wikidata.

Anyone can edit this tutorial or ask questions on the different talk-pages.

Installation edit

You can develop bots either online or offline.

Online, in your web browser (PAWS) edit

The tutorial (and any tasks beyond that) can also be completed using the PAWS webinterface.

This is the recommended option for most users:

Offline, installed on your computer edit

Introduction edit

This learning block focuses on setting up pywikibot, querying data, — using functions, iteration and generators, — and writing data to a file or database.

  1. Data Harvest: Gathering data from one Wikidata-item
  2. Winter Storage: Writing data into a file or database
  3. Big Data: Iterating over large sets of items

After finishing the first three chapters award yourself the {{User Pywikibot basic}} badge. Congratulations!

 This user has basic understanding of the Pywikibot software library.

Editing Wikidata edit

This learning block focuses on writing to Wikidata and comparing values between Wikipedia and Wikidata.

  1. Information about bot editing
  2. Labels: Setting labels, descriptions and aliases.
  3. Setting sitelinks
  4. Setting statements
  5. Changing Items: Correcting claims that link to a wrong item.
  6. Quantities and Units: Add a quantity claim with uncertainty and units.
  7. Setting qualifiers
  8. Setting sources

After finishing the 2nd block award yourself the {{User Pywikibot advanced}} badge. Congratulations!

 This user has an advanced understanding of the Pywikibot software library.

Expert topics and examples edit

This block consists of advanced examples that go beyond iterating over Wikidata and getting or setting statements. This includes for example the templates that are used on Wikidata. You can just read and try out the chapters that you are interested in.

  • Templates: Examples how to look for certain templates, statements within them, and editing them.

After finishing the 3rd block, or if you are already experienced with running bots on Wikidata, you can award yourself the {{User Pywikibot professional}} badge. Congratulations!

 This user is a Pywikibot expert.

Helpful Resources edit

BotAcademy 2015, Swedish

Workshops where this tutorial was used edit