pythonmediawikiwikipedia-apimediawiki-apipywikibot

How to get all archived talk pages of a wikipedia page?


I am using the following code to fetch talk page for a given Wikipedia page.

import pywikibot as pw
page = pw.Page(pw.Site('en'), 'Elon_Musk')
talkpage = page.toggleTalkPage()
talkpage.text

This works fine, but it does not return all archived talk pages. Is there a way I can programmatically find archives for a given talk page and loop through them to get the text?

Many thanks!


Solution

  • You can get all subpages with the following code:

    import pywikibot as pw
    site = pw.Site('en', 'wikipedia')
    for page in site.allpages(prefix='Elon Musk/', namespace='Talk'):
        print(page.title())
        print(page.text)
    

    There exists a page "Talk:Elon_Musk/FAQ". If you don't want to include this page and similar ones, you need to add an additional line before returning the text: if 'Archive' in page.title(): .