Python请求+代理+302重定向,为什么请求不正确?

标签 python proxy python-requests httprequest http-status-code-302

我正在使用 Python 请求并通过代理发送请求。我向其发送请求的站点有 302 重定向,请求无法正常工作。看起来请求没有代理发送,网站找​​到了我的真实 IP。

Python代码:

try:
    session = Session()
    request = Request('GET', url, headers=headers)
    prepped = session.prepare_request(request)
    resp = session.send(prepped, proxies=proxy, timeout=8)
    session.cookies.clear()
    print(resp.status_code)
    print(resp.history)
except requests.exceptions.Timeout:
    print("Timeout error ... :( " + "\n")
except requests.exceptions.ConnectionError:
    print("Connection error ... :( " + "\n")
except requests.exceptions.HTTPError:
    print("HTTPError ... :( " + "\n")

响应历史

<Response [302]>

基本上我需要从另一个 IP 发送请求,并且每次都是作为新用户使用新 cookie 等。 但是使用这段代码我无法做到。 有人可以帮我解决这个问题吗?

最佳答案

您可以使用requests.head 先获取重定向的url。

r = requests.head(url, allow_redirects=True) 打印(r.url)

关于Python请求+代理+302重定向,为什么请求不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29105989/

相关文章:

jquery - Hook 并更改 ajax 请求

python - 如何使用Python在Windows中设置代理?

python - 无法连接到雪花

python - 带有状态码 200 重定向的链接

python - 从模块内部运行 pytest,似乎缓存测试

python - "def post"和 "if.request.method == POST "是一样的吗?

Python - 数据模型 - 子类与实例

apache - 从 apache 服务中排除 404

python - 如何修复 "latin-1 codec can' t 在请求中的位置编码字符

python - 我可以在 Django 中访问与其他默认数据库不同的数据库吗