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

请教个 Requests 的问题

  •  
  •   cruelcage · 2014-03-04 20:08:44 +08:00 · 3291 次点击
    这是一个创建于 3712 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我想用Requests模拟登陆http://bbs.weiphone.com

    我看了下登陆要用到的cookie是xIka_2132_saltkey和xIka_2132_auth

    但是我post后得到的response.cookies里面没有这两个cookie,有人能告诉我漏了什么吗?

    login_url = 'http://passport.weiphone.com/?r=user/loginProcess'
    user = 'xxxx'
    password = 'xxxx'
    user_info = {'Kaf_Model_Form_Login[login]': user,
    'Kaf_Model_Form_Login[password]': password,
    'Kaf_Model_Form_Login[remember]': 0 }
    login = requests.post(login_url, data=user_info)
    for cookie in login.cookies:
    print (cookie.name, cookie.value)

    output: ('PHPSESSID', 'vc6v382ibq656ftmj4ej0jef26')
    8 条回复    1970-01-01 08:00:00 +08:00
    allenforrest
        1
    allenforrest  
       2014-03-04 21:37:15 +08:00 via iPhone
    为什么不用session?完全不管cookies了
    clino
        2
    clino  
       2014-03-04 22:23:22 +08:00
    如楼上所说,requests某版本以后response里就开始没有cookie了,session里一直都是有的
    http://docs.python-requests.org/en/latest/api/#request-sessions
    cruelcage
        3
    cruelcage  
    OP
       2014-03-04 23:38:32 +08:00
    @allenforrest
    @clino
    多谢,但是还是拿不到我要的cookie
    post后有个自动跳转的链接,不明白为什么会自己加上3个参数http://bbs.weiphone.com/api/uc.php?time=xxx&code=xxx&_=xxx,主要是那个code
    binux
        4
    binux  
       2014-03-04 23:44:47 +08:00
    print login.status_code, login.headers
    cruelcage
        5
    cruelcage  
    OP
       2014-03-05 08:26:03 +08:00
    @binux headers里面也没有啊

    'set-cookie': 'PHPSESSID=01d2bjtr95sk4otmok772rqss2; path=/, PHPSESSID=v591fg9v12a2hiv71mn7o70837;
    ushuz
        6
    ushuz  
       2014-03-05 08:50:13 +08:00 via iPhone
    @cruelcage
    状态码是3xx么?看headers里的location。2xx看response里有没有js跳转。
    allenforrest
        7
    allenforrest  
       2014-03-05 16:04:29 +08:00
    @cruelcage 这个不是 cookie 吧?
    cruelcage
        8
    cruelcage  
    OP
       2014-03-05 17:21:49 +08:00
    @ushuz headers里面没有location,跳转的js我再研究下


    @allenforrest 是有个跳转的url,不知道参数是怎么来的。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2315 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:12 · PVG 16:12 · LAX 01:12 · JFK 04:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.