Created
October 14, 2020 09:32
-
-
Save hardiksondagar/66cd56f7a86b30df61f6fa3d4d691c5d to your computer and use it in GitHub Desktop.
Get Redirected URL
This file contains hidden or 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
import requests | |
def get_redirect_url(url): | |
try: | |
headers = { | |
"User-Agent": " ".join([ | |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6)", | |
"AppleWebKit/537.36 (KHTML, like Gecko)", | |
"Chrome/86.0.4240.75", | |
"Safari/537.36" | |
]) | |
} | |
response = requests.get(url, headers=headers, allow_redirects=True) | |
if response.history: | |
return response.url | |
else: | |
return url | |
except Exception: | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment