As in this post, I attempt to get the final redirect of a webpage as:
如本文所述,我试图将网页的最终重定向设置为:
import urllib.request
response = urllib.request.urlopen(url)
response.geturl()
But this doesn't work as I get the "HTTPError: HTTP Error 300: Multiple Choices" error when attempting to use urlopen
.
但是,当我尝试使用urlopen时,我得到了“HTTPError: HTTPError 300:多个选择”错误。
See documentation for these methods here.
请参阅这里的这些方法的文档。
EDIT:
编辑:
This problem is different than the Python: urllib2.HTTPError: HTTP Error 300: Multiple Choices question, because they skip the error-causing pages, while I have to obtain the final destination.
这个问题不同于Python: urllib2。HTTPError: HTTP错误300:多个选择问题,因为它们跳过错误导致的页面,而我必须获得最终的目的地。
1 个解决方案
#1
0
As suggested by @abccd, I used the requests
library. So I will describe the solution.
正如@abccd所建议的,我使用了请求库。我来描述一下解。
import requests
url_base = 'something' # You need this because the redirect URL is relative.
url = url_base + 'somethingelse'
response = requests.get(url)
# Check if the request returned with the 300 error code.
if response.status_code == 300:
redirect_url = url_base + response.headers['Location'] # Get new URL.
response = requests.get(redirect_url) # Make a new request.
#1
0
As suggested by @abccd, I used the requests
library. So I will describe the solution.
正如@abccd所建议的,我使用了请求库。我来描述一下解。
import requests
url_base = 'something' # You need this because the redirect URL is relative.
url = url_base + 'somethingelse'
response = requests.get(url)
# Check if the request returned with the 300 error code.
if response.status_code == 300:
redirect_url = url_base + response.headers['Location'] # Get new URL.
response = requests.get(redirect_url) # Make a new request.