Manual:Pywikibot/Create your own script

From mediawiki.org
Revision as of 13:48, 17 October 2013 by Soshial (talk | contribs) (→‎Tips: updated link)

Tips

Here and in wikipedia.py, there are some very basic tips for getting started writing your own bot:

  • be sure you've set up your user-config.py file (see here)
  • To gain access to the pywikipedia framework, use:
import wikipedia
  • to retrieve a page, use the following, where pageName is, e.g., "Wikipedia:Bots" or "India":
site = wikipedia.getSite()
page = wikipedia.Page(site, u"pageName")
text = page.get(get_redirect = True)
  • to update a page, use:
page.put(u"newText", u"Edit comment")
  • look at some of the pywikipedia files for other ideas -- basic.py is relatively easy to read even if you're new to pywikipedia.
  • you can find all available Page methods in the wikipedia.py file.
  • basic.py gives you a setup that can be used for many different bots, all you have to do is define the string editing on the page text.
  • To iterate over a set of pages, see pagegenerators.py for some objects that return a set of pages. An example use of the CategoryPageGenerator that does something for each page in the Category:Living people category:
import catlib
import pagegenerators
import wikipedia
site = wikipedia.getSite()
cat = catlib.Category(site,'Category:Living people')
gen = pagegenerators.CategorizedPageGenerator(cat)
for page in gen:
  #Do something with the page object, for example:
  text = page.get()