Skip to content

Instantly share code, notes, and snippets.

@baskaufs
Created September 9, 2019 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baskaufs/4483157f07341d7de9005561355c3487 to your computer and use it in GitHub Desktop.
Save baskaufs/4483157f07341d7de9005561355c3487 to your computer and use it in GitHub Desktop.
Python script to use Wikidata Query Service to find Vanderbilt employees who have created works
import requests
def getWikidata(query):
endpointUrl = 'https://query.wikidata.org/sparql'
# The endpoint defaults to returning XML, so the Accept: header is required
r = requests.get(endpointUrl, params={'query' : query}, headers={'Accept' : 'application/sparql-results+json'})
data = r.json()
statements = data['results']['bindings']
return statements
# Main routine
inputResearcherName = input("What's the name of the researcher? ")
found = False
# get researcher names and URIs from the WikiData API
query = '''prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?person ?personLabel
where {
?article wdt:P50 ?person.
?person wdt:P108 wd:Q29052.
?person rdfs:label ?personLabel.
filter(lang(?personLabel)="en")
}
order by ?personLabel'''
researchers = getWikidata(query)
for researcher in researchers:
if inputResearcherName.lower() in researcher['personLabel']['value'].lower():
found = True
print('\n') # skip 2 lines
print(researcher['personLabel']['value'])
print()
researcherId = researcher['person']['value']
# get article names from the WikiData API
query = '''prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?articleLabel
where {
?article wdt:P50 <''' + researcherId + '''>.
?article rdfs:label ?articleLabel.
filter(lang(?articleLabel)="en")
}
order by ?articleLabel'''
articles = getWikidata(query)
for article in articles:
print(article['articleLabel']['value'])
if not found:
print("Didn't find that researcher")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment