python 获取网页header

来源:互联网 发布:网络流行语 四个字 编辑:程序博客网 时间:2024/05/19 20:42
因之前想写python爬虫判断douban的页面重定向,故写了以下代码,获取网页头部信息, 返回响应码
conn = httplib.HTTPConnection(host)conn.request("GET",path)return conn.getresponse().status
HTTPConnection.request(method, url[, body[, headers]])
代码中使用了"HEAD"的http请求方法,故只会返回头信息,而不返回网页实体。
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.
ref:http://www.python.org/doc//current/library/httplib.html#httpconnection-objects
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
原创粉丝点击