Cookie报错
#coding:utf-8
import urllib2
import cookielib
url="https://www.baidu.com"
print "打印第一种方法"
response1=urllib2.urlopen(url)
print response1.getcode()
print len(response1.read())
print "打印第二种方法"
request=urllib2.Request(url)
request.add_header("user-agent","Mozilla/5.0")
response2=urllib2.urlopen(request)
print len(response2.read())
print "打印第三种方法"
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3=urllib2.urlopen(url)
print response3.getcode()
print cj
print response3.read()
运行结果
{'username': 'admin', 'machine': ['foo', 'baz']}
{'username': 'mlh', 'machine': ['foo', 'baz']}
200
227
打印第二种方法
227
打印第三种方法
D:\Python\Python\lib\cookielib.py:1600: UserWarning: cookielib bug!
Traceback (most recent call last):
File "D:\Python\Python\lib\cookielib.py", line 1598, in make_cookies
parse_ns_headers(ns_hdrs), request)
File "D:\Python\Python\lib\cookielib.py", line 1555, in _cookies_from_attrs_set
cookie = self._cookie_from_cookie_tuple(tup, request)
File "D:\Python\Python\lib\cookielib.py", line 1548, in _cookie_from_cookie_tuple
rest)
File "D:\Python\Python\lib\cookielib.py", line 781, in __init__
self._rest = copy.copy(rest)
AttributeError: 'module' object has no attribute 'copy'
_warn_unhandled_exception()
200
<CookieJar[]>
<html>
<head>
<script>
location.replace(location.href.replace("https://","http://"));
</script>
</head>
<body>
<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>
</body>
</html>
[Finished in 1.5s]
为什么第一个print有误,且cookie打印不出来