Skip to content

Is it possible to get the wikidata ID from a Wikipedia article name? #49

Answered by dahlia
youssefavx asked this question in Q&A
Discussion options

You must be logged in to vote

This project does not directly cover Wikipedia, but you can use Wikipedia API to find a Wikidata ID:

https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&titles=Physics

>>> import json
>>> from urllib.parse import quote
>>> from urllib.request import urlopen
>>> title = 'Physics'
>>> url = f'https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&format=json&titles={quote(title)}'
>>> r = urlopen(url)
>>> pages = json.load(r)
>>> r.close()
>>> page, = pages['query']['pages'].values()
>>> wikibase_item = page['pageprops']['wikibase_item']
>>> wikibase_item
'Q413'

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dahlia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #23 on August 04, 2022 17:29.