推荐学习书目
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 · Jun 2, 2016 · 7290 views
    This topic created in 3657 days ago, the information mentioned may be changed or developed.

    以下是代码

    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 replies    2018-07-10 17:31:13 +08:00
    virusdefender
        1
    virusdefender  
       Jun 2, 2016
    模拟的还不像,被识别出来了。
    CBBing
        2
    CBBing  
    OP
       Jun 2, 2016
    @virusdefender 能指点一下吗?
    CBBing
        3
    CBBing  
    OP
       Jun 4, 2016
    各位别光收藏哈,给点参考意见
    lanshee
        4
    lanshee  
       Jul 10, 2018
    大侠后来呢?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2837 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 04:06 · PVG 12:06 · LAX 21:06 · JFK 00:06
    ♥ Do have faith in what you're doing.