Skip to content

Instantly share code, notes, and snippets.

@ptitzler
Created August 31, 2017 18:53
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 ptitzler/6b79e51d0dfb3f85a849e466dd006062 to your computer and use it in GitHub Desktop.
Save ptitzler/6b79e51d0dfb3f85a849e466dd006062 to your computer and use it in GitHub Desktop.
def getWikipediaURL(entity, exact_match_only = False):
# wikipedia search URL
url = 'https://en.wikipedia.org/w/index.php?{}'. \
format(urllib.urlencode({'search': entity},'utf-8'))
# send search request
r = requests.head(url)
if r.status_code == 200:
if exact_match_only:
# search term was not found; return nothing
return None
else:
# search term was not found; return search URL
return url
elif r.status_code == 302:
# search term was found; return redirect URL to wiki page
return r.headers['Location']
else:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment