小编Gui*_*Wei的帖子

python请求处理错误302?

我正在尝试使用requests库向重定向 url(在响应标头位置)发出 http 请求。使用 Chrome 检查时,我可以看到响应状态为 302。

但是,在 python 中,requests总是返回 200 状态。我添加了allow_redirects=False,但状态仍然始终为 200。

  • 网址是 https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A//oauth.weico.cc&response_type=code&client_id=211160679
  • 第一行进入测试账号: moyan429@hotmail.com
  • 第二行输入密码: 112358

然后点击第一个按钮登录。

我的 Python 代码:

import requests

user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36'
session = requests.session()
session.headers['User-Agent'] = user_agent
session.headers['Host'] = 'api.weibo.com'
session.headers['Origin']='https://api.weibo.com'
session.headers['Referer'] ='https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A//oauth.weico.cc&response_type=code&client_id=211160679'
session.headers['Connection']='keep-alive'

data = {
    'client_id': api_key,
    'redirect_uri': callback_url,
    'userId':'moyan429@hotmail.com',
    'passwd': '112358',
    'switchLogin': '0',
    'action': 'login',
    'response_type': 'code',
    'quick_auth': 'null'
}

resp = session.post(
    url='https://api.weibo.com/oauth2/authorize', …
Run Code Online (Sandbox Code Playgroud)

python redirect python-requests

2
推荐指数
1
解决办法
9748
查看次数

标签 统计

python ×1

python-requests ×1

redirect ×1