Get "also known as"-properties from wikidata in pywikibot

554 views Asked by At

im trying to extract the "also known as"-information from wikidata. E.g., by visiting the page of elvis(https://www.wikidata.org/wiki/Q303), i want to reach the information "Elvis, Elvis Aaron Presley, The King, The King Of Rock'n'Roll"

I'm using pywikibot as a script on Python 3.5.

Right know i can grab the text of the page (which seems not to contain these synonyms), as well as the Itempage with the translations on the right side.

import pywikibot
 site = pywikibot.Site('en', 'wikipedia')
page = pywikibot.Page(site, 'Elvis Presley')
item = pywikibot.ItemPage.fromPage(page)

item.get()  # you need to call it to access any data.
sitelinks = item.sitelinks
print(sitelinks)

Thank you in advance!

1

There are 1 answers

0
xqt On

Your code looks well. You have just to fetch the aliases instead of sitelinks, e.g.:

from pprint import pprint
pprint(item.aliases['en'])

which gives you the expected list:

['Elvis',
 'Elvis Aaron Presley',
 "The King of Rock'n'Roll",
 "King of Rock'n'Roll",
 'Elvis Aron Presley',
 "The King of Rock 'n' Roll",
 "King of Rock 'n' Roll",
 'The King',
 'Elvis A. Presley']