Wikidata:Pywikibot - Python 3 Tutorial

From Wikidata
Jump to navigation Jump to search

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎dansk • ‎français • ‎polski • ‎svenska • ‎čeština • ‎русский • ‎العربية • ‎日本語

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 four chapters award yourself the {{User Pywikibot basic}} badge. Congratulations!

Pwb icon.svgThis user has basic understanding of the Pywikibot.

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!

Pwb icon.svgThis user has advanced understanding of the Pywikibot.

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.
  • Page views: Get the page views from items and linked pages
  • Create Items: Bots can create missing items
  • Merging items
  • Get the sum of pages in a category or namespace
  • Upload images and write pages on Commons
  • More ideas needed
  • ...
  • ..
  • .

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!

Pwb icon.svgThis user is a Pywikibot expert.

Helpful Resources[edit]

Workshops where this tutorial was used[edit]