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

求教,这个模拟登陆为什么是 403?

  •  
  •   wex · 2016-04-22 20:06:45 +08:00 · 3078 次点击
    这是一个创建于 2926 天前的主题,其中的信息可能已经有所发展或是发生改变。
    9 条回复    2016-04-23 13:25:14 +08:00
    wex
        1
    wex  
    OP
       2016-04-22 20:07:15 +08:00
    wex
        2
    wex  
    OP
       2016-04-22 20:07:26 +08:00
    这个咋么编辑?
    wex
        3
    wex  
    OP
       2016-04-22 20:12:42 +08:00
    caspartse
        4
    caspartse  
       2016-04-23 00:15:04 +08:00
    eoo
        5
    eoo  
       2016-04-23 02:47:17 +08:00 via Android
    PHP 搞起
    wex
        6
    wex  
    OP
       2016-04-23 10:11:27 +08:00 via Android
    @caspartse 还是 403
    ming2281
        7
    ming2281  
       2016-04-23 10:24:13 +08:00   ❤️ 1
    与题目无关的几点
    找 login-url 时一定要找对,具体是查看 chrome 控制台的 Request-URL 那里
    使用 requests, 从此模拟登陆不要太简单!(幸福感大大提升) 读读其文档,也就一两小时, 神兵利器
    caspartse
        8
    caspartse  
       2016-04-23 11:03:03 +08:00
    @wex

    #!/usr/bin/env python
    # -*- coding:UTF-8 -*
    import requests
    import re
    import simplejson as json


    session = requests.Session()
    url = 'http://account.lvye.cn/accounts/login'
    response = session.get(url)
    content = response.content
    pattern = r'<input type=\'hidden\' name=\'csrfmiddlewaretoken\' value=\'([^\s]+?)\' />'
    token = re.search(pattern, content).group(1)
    data = {
    'username': 'ly233',
    'password': 'ly233233',
    'cap_code': '',
    'auto': 'on',
    'csrfmiddlewaretoken': token,
    'next': 'http://lvye.cn',
    'client_id': 'None',
    'state': 'None',
    'cap_key': '',
    'login_code': ''
    }
    url = 'http://account.lvye.cn/accounts/ajax_login/'
    response = session.post(url, data=data)
    content = response.content
    result = json.loads(content)
    print 'uid: %s , username: %s , email: %s' % (result.get('uid'), result.get('name'), result.get('email'))
    wex
        9
    wex  
    OP
       2016-04-23 13:25:14 +08:00
    @caspartse
    谢谢!!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2852 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:52 · PVG 10:52 · LAX 19:52 · JFK 22:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.