为了账号安全,请及时绑定邮箱和手机立即绑定

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打印不出来

正在回答

1 回答

应该是你使用的工具的原因,我在sublime打印不出来,但是在Pycharm打印出来了

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Python开发简单爬虫
  • 参与学习       227670    人
  • 解答问题       1219    个

本教程带您解开python爬虫这门神奇技术的面纱

进入课程

Cookie报错

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信