pythonmediawikiwikipediawikipedia-apimediawiki-api

How to get backlinks in certain pages in wikipedia in python?


I am using the following code to get the backlinks of a page in wikipedia.

import pywikibot as pw
backlinks_list = []

for item_backlink in pw.Page(pw.Site('en', 'wikipedia'), wikipedia_name).backlinks():
    backlinks_list.append(item_backlink.title())

However, when I set the wikipedia_name = "Cyproheptadine" in the code, I get the following error.

pywikibot.exceptions.CircularRedirect: Page [[en:Dibenzocycloheptene]] is a circular redirect.
CRITICAL: Exiting due to uncaught exception <class 'pywikibot.exceptions.CircularRedirect'>

I am wondering why this happens and how to get backlinks of such pages?

I am happy to provide more details if needed.


Solution

  • By default, .backlinks() includes backlinks of redirected pages. While this is sometimes a desired feature, it causes the error in your case. "Dibenzocycloheptene" is a backlink of "Cyproheptadine", but "Dibenzocycloheptene" is also a redirect to "Dibenzosuberane" which is again a redirect to "Dibenzocycloheptene". This is a circle and thus pywikibot throws an error.

    You can solve this problem by setting .backlinks(follow_redirects=False). Then backlinks of redirects will not be included in your list.

    As circular redirects are quite rare, you could also solve this problem at the source: go to Wikipedia and cut the circle by removing the redirect link on "Dibenzocycloheptene".