Mwclient: Replace text in all pages in a category

From Glitchdata
Jump to navigation Jump to search

What if we want to replace text on every page in an entire category tree? For this we use a simple recursive function which calls itself on sub-categories, as in this example, which replaces all "cc-by-2.0" license tags with "cc0" in a given category tree on Commons:

import mwclient

def replace_in_category(category):
    print 'Replacing in category ' + category.[[Page.page_title|page_title]]
    for page in category:
        if page.namespace == 14:  # 14 is the category namespace
            print page.page_title
            text = page.text()
            text = text.replace('{{cc-by-2.0}}', '{{cc0}}')
  , summary='Replacing license tag ({{cc-by-2.0}} -> {{cc0}})')
    print 'Done with category ' + cat.page_title

site = mwclient.Site(('https', ''))
site.login('username', 'password')
replace_in_cat(site.Categories['Root category'])