推荐学习书目
› 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
ray1888
V2EX  ›  Python

使用 Python 原生库如何复用 cookie

  •  
  •   ray1888 ·
    ray1888 · Aug 13, 2017 · 3061 views
    This topic created in 3381 days ago, the information mentioned may be changed or developed.

    现在在写一个需求类似于爬虫的小脚本,然后那个软件是用 http authentacion 进行认证的 想问问大佬们,我用 cookielib 把 cookie 保存之后,如何再去使用他来登陆网站应用? 现在的问题是不能复用 cookie 的我,我直接访问,会出现 401 认证错误。所以如何解决这个问题呢?

    10 replies  •  2017-08-14 13:35:51 +08:00
    ray1888
    ray1888
    OP
    Aug 13, 2017
    1
    登陆的部分函数是这样的:
    base64string = base64.encodestring(
    "%s:%s" % (username, password))[:-1] # 注意哦,这里最后会自动添加一个\n
    authheader = "Basic %s" % base64string
    cookie = cookielib.CookieJar()
    handler = urllib2.HTTPCookieProcessor(cookie)
    opener = urllib2.build_opener(handler)
    req = urllib2.Request(url=url)
    req.add_header("Authorization", authheader)
    response = opener.open(req)
    ihciah
    ihciah
    Aug 13, 2017 via iPad
    2
    无脑 pickle 吧
    golmic
    golmic
    Aug 13, 2017
    3
    为什么一定要用原声库呢? requests 不好么?
    wolong
    wolong
    Aug 13, 2017
    4
    用 requests 很方便啊。
    ray1888
    ray1888
    OP
    Aug 13, 2017
    5
    @golmic 环境依赖问题,要求不能用第三方的库
    linhua
    linhua
    Aug 13, 2017
    6
    pickle 序列化、反序列化
    tempdban
    tempdban
    Aug 14, 2017 via Android
    7
    不说别的 你知道为啥会加\n 么 你用错了
    lieh222
    lieh222
    Aug 14, 2017
    8
    http authentacion 认证的一般不用管 cookie 了,所有请求头部都带上认证信息就行了
    Luckyray
    Luckyray
    Aug 14, 2017
    9
    不能用第三方库?难道用的 ios 上的 Pythonista ?
    fisher335
    fisher335
    Aug 14, 2017
    10
    python 牛 B 的地方就在第三方库~ 不让用的话的确是扬短避长啊~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2521 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 04:44 · PVG 12:44 · LAX 20:44 · JFK 23:44
    ♥ Do have faith in what you're doing.