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

尝试用 python 模拟登录,结果卡在 formhash 上。请大神指点一下

  •  
  •   namaste ·
    zy-lee · 2014-07-19 23:15:08 +08:00 · 4414 次点击
    这是一个创建于 3540 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想用python登这个站抓取订单--> http://www.kadang.com/index.php?act=login&op=seller 卡在登录上,post项里有个formhash,google了一下都是关于discuz的,但我用beautifulsoup抓出来的hash都是失效的。发现网页每次刷新一下,formhash就变一次,有什么办法post正确的formhash呢。我这分析对么,菜鸟求指点
    5 条回复    2014-07-20 10:57:45 +08:00
    izoabr
        1
    izoabr  
       2014-07-19 23:21:25 +08:00
    你用beautifulsoup抓出来的跟你python里面用的是两个session,我估计这个formhash值是跟session绑了的。
    是不是可以用liburl来获取一下网页,然后取值去,再拼接登陆参数
    xingzhi
        2
    xingzhi  
       2014-07-20 01:28:37 +08:00
    先get页面,获取formhash与cookies,然后post时带上cookies

    fyi:http://docs.python-requests.org/en/latest/user/quickstart/#cookies
    xingzhi
        3
    xingzhi  
       2014-07-20 01:30:01 +08:00   ❤️ 1
    另外,注意post时的地址是这个: http://www.kadang.com/index.php?act=login&op=logincheck

    op参数的值不一样
    pandada8
        4
    pandada8  
       2014-07-20 05:26:21 +08:00 via Android
    用python-requests然后开一个requests.session()
    yingluck
        5
    yingluck  
       2014-07-20 10:57:45 +08:00
    楼上正解
    import requests

    s = requests.Session()
    s.get('http://the.url.to.login')
    s.post(url)

    但是requests抓取的coockie都是cookieJar的类,不明白怎么存进数据库,然后每次调用都用保存的cookie获得session?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2762 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:04 · PVG 20:04 · LAX 05:04 · JFK 08:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.