V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sdlearn
V2EX  ›  Python

Python 中如何禁用 cookies

  •  
  •   sdlearn · 2017-02-06 01:44:49 +08:00 · 3608 次点击
    这是一个创建于 2640 天前的主题,其中的信息可能已经有所发展或是发生改变。

    由于 python 程序中需要登录操作所以在一个函数中通过以下方式启用了 cookies :

    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    

    但是登录完成后其他的一些操作不再需要 cookies 了,而且 cookies 越少越好,所以我想再禁用了它,该如何操作呢

    5 条回复    2017-02-25 15:04:14 +08:00
    xiaket
        1
    xiaket  
       2017-02-06 06:16:28 +08:00
    遇到这种用 urllib/urllib2 的情形,永远推荐 requests.
    XYxe
        2
    XYxe  
       2017-02-06 08:44:34 +08:00 via Android
    别 install_opener 嘛,需要 cookie 的时候用 opemer ,不需要的时候直接用 urllib2.urlopen 。
    用 requests 也差不多,需要的时候用 session ,不需要的时候直接用 requests.get 。
    lightening
        3
    lightening  
       2017-02-06 21:00:20 +08:00
    不知道如何禁用 cookies ,不过你确定登录完成后就不需要 cookie 了么,那么登录状态是如何保存的呢?
    zhtsuc
        4
    zhtsuc  
       2017-02-07 11:36:20 +08:00
    你不写 cookie ,或者请求的时候不发 cookie 不就行了
    sdlearn
        5
    sdlearn  
    OP
       2017-02-25 15:04:14 +08:00
    @lightening 登录上执行完想要的操作(这步需要 cookies )后就不需要登录状态了,后面的操作需要 cookies 越少越好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5844 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:44 · PVG 10:44 · LAX 19:44 · JFK 22:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.