用python获取http响应代码

d-c*_*d-c 4 python http

我知道如何与httplib的做到这一点,但我还需要设置用户代理,我敢肯定,你需要的urllib做到这一点.如何使用urllib获取http响应代码?

car*_*arl 5

您可以.getcode()在urllib2 中使用以获取HTTP代码:

urllib2.urlopen("http://google.com").getcode()
Run Code Online (Sandbox Code Playgroud)

完整标题包含在info()列表中:

urllib2.urlopen("http://google.com").info().headers
Run Code Online (Sandbox Code Playgroud)