--Get the URL of the HTTP redirect destination in Python 3 --Operation check environment: Python 3.8.5 + macOS Catalina
Save the following contents with the file name get_redirect.py.
get_redirect.py
import sys
import urllib.request
#Handler class that does not redirect
class NoRedirectHandler(urllib.request.HTTPRedirectHandler):
# HTTPRedirectHandler.redirect_Override request
def redirect_request(self, req, fp, code, msg, hdrs, newurl):
self.newurl = newurl #Hold redirect URL
return None
#Function to get the redirect URL
def get_redirect_url(src_url):
#Set handlers that do not redirect
no_redirect_handler = NoRedirectHandler()
opener = urllib.request.build_opener(no_redirect_handler)
try:
with opener.open(src_url) as res:
return None #It was a URL that didn't redirect
except urllib.error.HTTPError as e:
if hasattr(no_redirect_handler, "newurl"):
return no_redirect_handler.newurl #Returns the redirect URL
else:
raise e #Rethrow because it is an exception that occurred other than the redirect
#Get command line arguments
src_url = sys.argv[1]
#Get the redirect URL
redirect_url = get_redirect_url(src_url)
#Output redirect URL
if redirect_url is not None:
print(redirect_url)
Execution example.
$ python get_redirect.py https://bit.ly/3kmTOkc
https://t.co/yITSBp4ino
$ python get_redirect.py https://t.co/yITSBp4ino
https://qiita.com/niwasawa
$ python get_redirect.py https://qiita.com/niwasawa
Save the following contents with the file name get_redirect.py.
get_redirect.py
import sys
import urllib.request
#Function to get the redirect URL
def get_redirect_url(src_url):
with urllib.request.urlopen(src_url) as res:
url = res.geturl() #Get the final URL
if src_url == url:
return None #Not redirected because it is the same as the specified URL
else:
return url #Redirecting because it is different from the specified URL
#Get command line arguments
src_url = sys.argv[1]
#Get the redirect URL
redirect_url = get_redirect_url(src_url)
#Output redirect URL
if redirect_url is not None:
print(redirect_url)
Execution example. In the simplified version, a request is sent to the redirect destination URL, and in the case of multi-stage redirect, the final URL is output.
$ python get_redirect.py https://bit.ly/3kmTOkc
https://qiita.com/niwasawa
$ python get_redirect.py https://t.co/yITSBp4ino
https://qiita.com/niwasawa
$ python get_redirect.py https://qiita.com/niwasawa
-[urllib \ .request ---extensible library for opening URLs — Python 3 \ .8 \ .5 documentation](https://docs.python.org/ja/3.8/library/urllib .request.html) --How to get resources on the internet using the urllib package — Python 3 \ .8 \ .5 documentation -HTTP Redirection -HTTP \ | MDN
Recommended Posts