【问题标题】:Can`t connect to a proxy using requests Python无法使用请求 Python 连接到代理
【发布时间】:2021-05-05 18:57:19
【问题描述】:

我正在尝试使用 python 连接到 http 服务器,但在我发送 get 请求到:https://httpbin.org/ip

如果我没有使用代理,我可以公开正常的 ip。

我们假设我没有使用代理的公共 ip 是:10.10.10.10 这是我的代码:

proxies ={
        
    "http":"http://103.103.175.253:3128"
}
get = requests.get("https://httpbin.org/ip", proxies = proxies)
soup = bs(get.text,'html.parser')
print(soup.prettify())
print(get.status_code, get.reason)

我得到:

{
  "origin": "10.10.10.10"
}

200 OK

我应该收到 "origin":"103.103.175.253"

有人可以帮帮我吗????

【问题讨论】:

    标签: python-3.x web-scraping python-requests proxies


    【解决方案1】:

    您正在连接到https:// 站点,但您只指定了http 代理。

    您可以使用http:// 协议,或指定另一个https 代理。例如:

    proxies = {
        "http": "http://103.103.175.253:3128",
    }
    
    get = requests.get("http://httpbin.org/ip", proxies=proxies)  # <-- connect to http://
    soup = BeautifulSoup(get.text, "html.parser")
    print(soup.prettify())
    print(get.status_code, get.reason)
    

    打印:

    {
      "origin": "103.103.175.253"
    }
    
    200 OK
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-03
      • 2017-01-06
      • 2020-02-17
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多