用Python获取HTTP响应代码
我知道怎么用httplib来做到这一点,但我还需要设置用户代理(user-agent),我确定你需要用urllib来实现这个。那我该怎么用urllib获取HTTP响应代码呢?
2 个回答
0
其实,httplib 是可以设置 User-Agent 的。
headers = { 'User-Agent' : 'someapp', 'Content-Type' : 'text/html' }
conn = httplib.HTTPConnection(host, port)
conn.request('POST', '/foobar', 'mydata', headers)
5
你可以在urllib2中使用 .getcode()
来获取HTTP状态码:
urllib2.urlopen("http://google.com").getcode()
完整的头信息可以通过 info()
以列表的形式获取:
urllib2.urlopen("http://google.com").info().headers