Hoppa till innehållet

Projekt:Botacademy 2015/Pywikibot part 2

Från Wikimedia

Installation

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

Documentation

In depth documentation over at https://doc.wikimedia.org/pywikibot/index.html

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

Three easy ways in

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

Read and write to a wikipedia page

python pwb.py 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
>>> page.save(u"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
True
>>> 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 basic.py

Look at the source

...

Try python pwb.py basic -dry

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

Try python pwb.py basic -help

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

Try python pwb.py basic -weblink:oldlink.com -dry

Pagegenerators

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)