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
dancingbear
V2EX  ›  Python

如何给urllib2设置自己的cookie

  •  
  •   dancingbear · 2014-01-13 17:08:03 +08:00 · 7071 次点击
    这是一个创建于 3753 天前的主题,其中的信息可能已经有所发展或是发生改变。
    针对一个url,已经通过 cookielib.CookieJar 以及urllib2.HTTPCookieProcessor(cj), 初始化过urllib2,可以正常使用。
    现在想给个这cj添加一些自己的cookie,应该怎么做?
    3 条回复    1970-01-01 08:00:00 +08:00
    nervouna
        1
    nervouna  
       2014-01-13 18:27:22 +08:00
    建议使用第三方库 requests
    关于 Cookie:
    http://www.python-requests.org/en/latest/user/quickstart/#cookies

    urllib2 用起来有时候真的需要勇气……
    Lycnir
        2
    Lycnir  
       2014-01-13 19:30:59 +08:00
    支持LS
    dancingbear
        3
    dancingbear  
    OP
       2014-01-14 09:56:49 +08:00
    谢谢楼上,发现通过下列方式可以设置
    ck = cookielib.Cookie(version=0, name='cookie名称', value='值', port=None, port_specified=False, domain='www.abc.cn', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
    cj.set_cookie(ck)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3278 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:55 · PVG 20:55 · LAX 05:55 · JFK 08:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.