There are about 300 https addresses listed. It will be redirected to a different address as soon as I access it, so how can I get that address?
In the case of http, it is said that the following can be done
import urllib2
def get_redirected_url(url):
opener = urllib2.build_opener(urllib2.HTTPRedirectHandler)
request = opener.open(url)
return request.url
print get_redirected_url ("http://address")
It's https
urlib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (
).
It's a self-answer.
import urllib
import ssl
def get_redirected_url(url):
context = ssl._create_unverified_context()
return urllib.urlopen(url, context=context)
print get_redirected_url ("https://address").geturl()
It's working like this.
© 2024 OneMinuteCode. All rights reserved.