【问题标题】:How do I make the proxy work? Python requests如何使代理工作? Python 请求
【发布时间】:2021-03-07 03:08:25
【问题描述】:
import requests

http_proxy = "http://176.9.119.170:8080"

proxyDict = {
  "http": http_proxy,
}
url = 'http://2ip.ru'
s = requests.Session()
s.proxies = proxyDict
r = s.get(url)
print(r.text, r.url, r.headers)

此代码给出“requests.exceptions.TooManyRedirects: Exceeded 30 redirects”。错误。

另外,我之前尝试在 selenium 中使用代理,但出现了类似的错误。此外,即使有工作代理(由检查员确认)

【问题讨论】:

  • http://2ip.ru 将您重定向到https://2ip.ru/,但您仅为http 协议设置了代理。您应该将 https 键添加到具有相同值的 proxyDict 并查看您的代理,可能重定向问题在代理端。
  • @OlvinR​​oght 我将代理更改为 https 并编辑 proxyDict。但现在它返回“最大重试次数超过 url: / (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] 错误版本号 (_ssl.c:1076)')))”
  • 您不需要更改http_proxy 的值,只需向proxyDict 添加一个具有相同值的键,它看起来像:proxyDict = dict.fromkeys(("http", "https"), http_proxy)。再次检查您的代理,99% 的问题出在代理上。
  • 也许有什么方法可以使用http?我找不到正常的免费 https 代理。
  • 这个特定网站没有http版本,也许您可​​以找到另一个具有类似功能的网站,让您可以通过http协议工作。

标签: python web redirect proxy python-requests


【解决方案1】:

此表格的正确代码:

import requests

http_proxy = "http://176.9.119.170:8080"

proxyDict = {
  "http": http_proxy,
}
url = 'http://2ip.ru'
s = requests.Session()
r = s.get(url, proxies=proxyDict)
print(r.text, r.url, r.headers)

【讨论】:

  • requests.exceptions.TooManyRedirects:超过 30 个重定向
  • 这个错误是指你的url,'2ip.ru'有重定向,不是代码,代码工作正常,尝试其他url检查。
  • 仍然是重定向。即使在 Google 和任何其他网站上
  • 所以这是你的 http 代理,试试别的吧
  • 也许您可以展示一个带有工作代理的工作示例?我已经尝试过十几个不同的 http 代理,它们被标记为在各种检查器上工作。
猜你喜欢
  • 2012-09-18
  • 1970-01-01
  • 2021-10-27
  • 2014-08-03
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
相关资源
最近更新 更多