我在python中编写了一个程序来自动从网站检索信息。但它返回了很多响应消息,所以我无法看到我的程序如何处理。我怎么能把它关掉?
代码的一部分
cookiejar = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cookiejar)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler(debuglevel=1))
urllib2.install_opener(opener)
....
url = 'http://weibo.com/2344328334/fans'
header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36'}
req = urllib2.Request(url = url, headers = header)
result = urllib2.urlopen(req)
text = result.read()
输出的一部分
send: 'GET /2344328334/fans HTTP/1.1\r\nAccept-Encoding: ...'
reply: 'HTTP/1.1 302 Moved Temporarily\r\n'
header: Server: nginx
header: Date: Tue, 10 Sep 2013 03:54:02 GMT
header: Content-Type: text/html; charset=utf-8
header: Transfer-Encoding: chunked
header: Connection: close
header: Cache-Control: no-cache, must-revalidate
header: Expires: Sat, 26 Jul 1997 05:00:00 GMT
... other messages ...
答案 0 :(得分:1)
您打开调试选项,只需删除debuglevel=1
我推荐anather python http lib:requests,它非常易于使用且功能强大。只需安装pip install requests
,支持python2和python3
International Domains and URLs
Keep-Alive & Connection Pooling
Sessions with Cookie Persistence
...