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

python Requests 模拟登录知乎出现<Response [403]>?

  •  
  •   CBBing · 2016-06-02 11:53:57 +08:00 · 6720 次点击
    这是一个创建于 2895 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下是代码

    import requests
    from bs4 import BeautifulSoup as bs
    
    zhihu='https://www.zhihu.com/#signin'
    
    def login():
        header = {
            "Accept": "*/*",
            "Accept-Encoding": "gzip,deflate",
            "Accept-Language": "en-US,en;q=0.8,zh-TW;q=0.6,zh;q=0.4",
            "Connection": "keep-alive",
            "Content-Type": " application/x-www-form-urlencoded; charset=UTF-8",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36",
            "Referer": "http://www.zhihu.com/"
        }
        s = requests.session()
        cookie = bs(s.get('https://www.zhihu.com', headers=header).content).find(type='hidden')['value']
        print(cookie)
        post_data = {'_xsrf': cookie,  'password': 'xxx','captcha_type':'cn','remember_me': True,'email': 'xxxx'}
        p=s.post(url=zhihu, data=post_data, headers=header)
        print(p)
        r = s.get(url=zhihu)
       
    
    if __name__ == '__main__':  # 程序运行入口
        login()
    

    请问出现 403 错误的原因?

    4 条回复    2018-07-10 17:31:13 +08:00
    virusdefender
        1
    virusdefender  
       2016-06-02 12:00:05 +08:00
    模拟的还不像,被识别出来了。
    CBBing
        2
    CBBing  
    OP
       2016-06-02 12:00:42 +08:00
    @virusdefender 能指点一下吗?
    CBBing
        3
    CBBing  
    OP
       2016-06-04 12:52:44 +08:00
    各位别光收藏哈,给点参考意见
    lanshee
        4
    lanshee  
       2018-07-10 17:31:13 +08:00
    大侠后来呢?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5923 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 03:19 · PVG 11:19 · LAX 20:19 · JFK 23:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.