Inlinks and outlinks for a Wikipedia page in python

Viewed 362

I am using the Wikipedia API for python to get page links as follows:

import wikipedia
ny = wikipedia.page(wikipedia.search("Barack Obama")[0])
print ny.links

This ny.links provides all the links (inlinks and outlinks together), but I need them separately. Could anyone please help me finding some means to achieve this using the same API or some other APIs?

1 Answers

You can use wikipedia-api to get information about inlinks with property backlinks and outlinks with property links.

Code rewritten with Wikipedia-API would look like this:

import wikipediaapi
wiki = wikipediaapi.Wikipedia('en')
obama = wiki.page("Barack Obama")

# does it exists?
repr(obama.exists())

# show basic info
repr(obama)
# > 'Barack Obama (id: 534366, ns: 0)'

# inlinks
repr(obama.backlinks)
len(obama.backlinks)
# > 28323

# outlinks
repr(obama.links)
len(obama.links)
# > 2138
Related