Manual:Pywikibot/Recipes

From mediawiki.org

On this page you will find short snippets of code showing the framework usage by the various bots.

Get language links for all pages[edit]

Iterate through wiki pages (possibly with additional filters), and for each page get all listed languagelinks.

query&generator=allpages&gapfrom=T&prop=langlinks
  • API langlinks limits could make result incomplete for any page without any notification, making batch processing much more difficult - client needs to do more querycontinue calls to know there are no more langlinks for the specific page.


Pywikibot
# Please write the proper code snippet here


Get language links for a set of pages[edit]

For a user-supplied list of titles, resolve redirects and get their language links.

query&prop=langlinks&titles=T|TT&redirects
Pywikibot
# Please write the proper code snippet here


Get non-resolved links and categories for a set of redirected pages[edit]

For each page in a set, if redirect, resolve to the redirect target, and get all links and all categories listed. Only link's namespace+title is needed.

query&prop=links|categories&titles=Archiver|Abstract%20(law)&pllimit=300&redirects
Pywikibot
# Please write the proper code snippet here


Page generator -> get links and categories[edit]

From all pages in a wiki (possibly filtered), get all links and all categories listed. Only link's namespace+title is needed. Result pages (not links) might need to be edited.

query&generator=allpages&gapfrom=T&prop=links|categories
Pywikibot
# Please write the proper code snippet here


Modify a page[edit]

Given a page title, get the page content (if redirect, get the target), change and save it.

query&titles=Think&redirects&prop=revisions&rvprop=content
edit&summary=...&text=...&token=...
Pywikibot
# This sample does not resolve redirects. Need more feedback.
page = pywikibot.Page(pywikibot.Site('en'), 'Think')
oldcontent = page.get()
newcontent = oldcontent.replace('this', 'that')
page.put(newcontent, 'Bot: Edit')


If you need more help on setting up your Pywikibot visit the #pywikibot IRC channel connect or pywikibot@ mailing list.