bgo*_*ves 5 python http http-headers
我需要在没有实际下载内容的情况下检查页面是否被重定向.我只需要最终的URL.这样做的最好方法是Python?谢谢!
如果您特别想避免下载内容,则需要使用HEAD请求方法.我相信urllib和urllib2库不支持HEAD请求,因此您必须使用较低级别的httplib库:
import httplib
h = httplib.HTTPConnection('www.example.com')
h.request('HEAD', '/')
response = h.getresponse()
// Check for 30x status code
if 300 <= response.status < 400:
// It's a redirect
location = response.getheader('Location')
Run Code Online (Sandbox Code Playgroud)