如何使用 python 仅 urllib 库跟踪或检查重定向 URL 的历史记录
How to trace or to check history of redirected URLs with python only urllib library
当您在 6 次重定向后转到 https://httpbin.org/redirect/6 时,您将转到 https://httpbin.org/get
。我想检查中间的 URL - 仅使用 python urllib.request.
import urllib.request
def openurl(url):
headers = {}
req = urllib.request.Request(url, headers=headers)
httpResponse = urllib.request.urlopen(req)
code = httpResponse.getcode()
httpHeader = httpResponse.info()
httpBody = httpResponse.read().decode()
return httpHeader, httpBody, code
url = 'https://httpbin.org/redirect/6'
h, b, c = openurl(url)
print(h)
print(b)
print('http Response Code:', c)
是否有任何方法可以调整 urlopen 的行为以生成介于两者之间的 URL 列表?
P.S。我不能给你的答案投票,因为我的声誉低于 15,否则除非我再获得 4 分。
构建您自己的 HTTPRedirectHandler
:
是一项简单的任务
import urllib.request
class MyHTTPRedirectHandler(urllib.request.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
print("newurl", headers["location"])
return super().http_error_302(req, fp, code, msg, headers)
opener = urllib.request.build_opener(MyHTTPRedirectHandler)
urllib.request.install_opener(opener)
response = urllib.request.urlopen('https://httpbin.org/redirect/6')
response.read()
同意 georgexsh,
但您也可以修改 HTTPRedirectHandler
如下更短:
class MyHTTPRedirectHandler(urllib.request.HTTPRedirectHandler):
pass
当您在 6 次重定向后转到 https://httpbin.org/redirect/6 时,您将转到 https://httpbin.org/get
。我想检查中间的 URL - 仅使用 python urllib.request.
import urllib.request
def openurl(url):
headers = {}
req = urllib.request.Request(url, headers=headers)
httpResponse = urllib.request.urlopen(req)
code = httpResponse.getcode()
httpHeader = httpResponse.info()
httpBody = httpResponse.read().decode()
return httpHeader, httpBody, code
url = 'https://httpbin.org/redirect/6'
h, b, c = openurl(url)
print(h)
print(b)
print('http Response Code:', c)
是否有任何方法可以调整 urlopen 的行为以生成介于两者之间的 URL 列表?
P.S。我不能给你的答案投票,因为我的声誉低于 15,否则除非我再获得 4 分。
构建您自己的 HTTPRedirectHandler
:
import urllib.request
class MyHTTPRedirectHandler(urllib.request.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
print("newurl", headers["location"])
return super().http_error_302(req, fp, code, msg, headers)
opener = urllib.request.build_opener(MyHTTPRedirectHandler)
urllib.request.install_opener(opener)
response = urllib.request.urlopen('https://httpbin.org/redirect/6')
response.read()
同意 georgexsh,
但您也可以修改 HTTPRedirectHandler
如下更短:
class MyHTTPRedirectHandler(urllib.request.HTTPRedirectHandler): pass