python捕获web重定向



我的问题是关于Web重定向,我使用urllib>getcode()来了解返回的状态代码

这是我的代码

import urllib
a = urllib.urlopen("http://www.site.com/incorrect-tDirectory")
a.getcode()

a.getcode()返回200,但实际上它被重定向到主页,我检查了一些参考资料,说重定向应该返回,因为我记得300301,但它不是200,希望你能找到我所以我的问题是如何捕获重定向

urllib2.urlopen()文档页面显示:

此函数返回一个类似文件的对象,并带有两个附加方法:

geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed
info() — return the meta-information of the page, such as headers, in the form of an mimetools.Message instance (see Quick Reference to HTTP Headers)

urllib.urlopen()实际上也实现了geturl(),但它并没有明确地放在文档中。

最新更新