Created
August 31, 2017 18:53
-
-
Save ptitzler/6b79e51d0dfb3f85a849e466dd006062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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