4

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?

Joel
  • 1,564
  • 7
  • 12
  • 20
Krishnendu Ghosh
  • 314
  • 4
  • 21

1 Answers1

5

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
Martin Majlis
  • 363
  • 2
  • 10