Projekt:Botacademy 2015/Pywikibot part 2

Från Wikimedia
Hoppa till navigering Hoppa till sök


  • Has everyone managed to install pywikibot?
  • Has everyone managed to set up their config
    • python login or
    • python clean_sandbox -simulate
  • Explain the -simulate flag.
  • More global options which might be good to know.
  • Remember to regularly update (git pull)


In depth documentation over at

Also worth looking directly into pywikibot/ if you want to see all actions

Three easy ways in

  1. python shell
  2. import pywikibot (if it is in your $PYTHONPATH)
  3. use scripts/ as a starting point for building a script

Read and write to a wikipedia page

python shell -family:wikipedia -lang:sv

Welcome to the Pywikibot interactive shell!

>>> site = pywikibot.Site()
>>> page = pywikibot.Page(site, u"Användare:André Costa (WMSE)/pywikitests")
>>> text = page.text
>>> print text

>>> text = text.replace(u'elelr', u'eller')
>>> page.text = text
>>>"BotAcademy test")
Page [[Användare:André Costa (WMSE)/pywikitests]] saved

Adapted from Manual:Pywikibot/Create your own script

Read and write to a Wikidata page

python -i

>>> import pywikibot
>>> site = pywikibot.Site("wikidata", "wikidata")
>>> repo = site.data_repository()
>>> item = pywikibot.ItemPage(repo, u"Q4115189") # the wikidata sandbox item
>>> item.exists() # .get() loads and returns the contents, .exists() loads and returns bool
>>> print(item.sitelinks[u'enwiki'])
Wikipedia:Wikidata/Wikidata Sandbox
>>> claim = pywikibot.Claim(repo, u'P19') # birthplace
>>> target = pywikibot.ItemPage(repo, u"Q1754") # Stockholm
>>> claim.setTarget(target)
>>> item.addClaim(claim)

More examples over at Wikidata:Creating a bot#Pywikibot

Or the short course over at Wikidata:Pywikibot - Python 3 Tutorial

A look at

Look at the source


Try python basic -dry

Ok that didn’t work, we need to specify how it is going to find its pages

Try python basic -help

Ok there are a LOT of different generators. What about weblink?

Try python basic -dry


Handle the underlying API calls.

Generators so you can easily iterate over the results

The Preloading generator makes sure the pages are fetched more effectively (remember how we said you should ask for multiple pages in each API call)