【问题标题】:Python: HTTPS using a proxy server with requestsPython:HTTPS 使用带有请求的代理服务器
【发布时间】:2019-04-18 01:36:05
【问题描述】:

我正在使用 MyIPHide。我下载了他们的客户端软件,安装并开启了服务。

我可以使用浏览器正常访问https 网站,但我无法使用requests 获取页面

这行得通:

import requests
IP=requests.get('http://api.ipify.org').text

proxyDict = { "http"  : IP,
              "https"  : IP
            }

url='http://www.cnn.com'
r=requests.get(url,proxies=proxyDict)

这不是:

url='https://www.cnn.com'
r=requests.get(url,proxies=proxyDict)

唯一的区别是 httphttps

这是回溯:

File "C:\Python27\lib\site-packages\requests\adapters.py", line 502, in send
    raise ProxyError(e, request=request)
ProxyError: HTTPSConnectionPool(host='www.cnn.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', error(10053, 'An established connection was aborted by the software in your host machine')))

我尝试了其他https 网站,它们都不起作用。

我还向 MyIPHide 发送了电子邮件支持。他们说所有代理都支持https,当我只使用浏览器时确实如此。

一种可行的解决方法是,如果我使用 Selenium 并获取页面,然后使用 driver.page_source 获取文本。

这不是代理服务器的问题,因为我通过 sslprivateproxy.com 购买了一个私有代理服务器地址并输入了 IP 和端口,但仍然出现相同的错误。

我正在使用 Python 2.7.15 并请求 2.20.1。请求的非代理使用有效,即:

import requests
url='https://www.cnn.com'
r=requests.get(url)

>>> r
<Response [200]>
>>>

还尝试了带有请求 2.20.1 的 python 3.6 --> 相同的结果。

【问题讨论】:

  • 你添加端口了吗? {"https":"ip:port"}。或者试试{"https":"183.88.219.163:57457"}
  • 我认为你不需要。没有指定端口。
  • 如果我的代理为您工作,这意味着问题出现在您的代理上
  • @kcorlidy。感谢代理。看起来它也不起作用。 ProxyError: HTTPSConnectionPool(host='www.cnn.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('&lt;urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003CD9208&gt;: Failed to establish a new connection: [Errno 10061] No connection could be made because the target machine actively refused it',)))
  • 是的,并不是所有的节点都适合我。但您也可以在free-proxy-list.net 上找到一个。如果还是出错。告诉我您的平台、Python 版本和请求版本,以便我可以得到与您相同的错误然后找到解决方案。

标签: python https proxy python-requests


【解决方案1】:

根据 PoolManager 的大小请求它,所以如果你想在池中有更多的连接,你可以重置 HTTPAdapter() 的大小。这样的方式(以确保它是否有效,您可以设置为 0)。

import requests
from requests.adapters import HTTPAdapter

proxy = {"http":"118.174.233.31:51726",
        "https":"43.254.132.86:50659"} # free proxy,often need to modify

with requests.Session() as se:
    # pool_connections=pool_maxsize=0 -> pool closed
    se.mount('https://', HTTPAdapter(pool_connections=100,pool_maxsize=100)) 
    se.mount('http://', HTTPAdapter(pool_connections=100,pool_maxsize=100))
    print(se.adapters["https://"]._pool_maxsize)
    #print(se.get("https://github.com"))
    print(se.get('http://api.ipify.org',proxies=proxy).text)

#100
#118.174.233.31

【讨论】:

  • 在 Windows10、Python27、requests-2.20.1 上运行
  • 感谢您的尝试,但我在发出页面请求时仍然遇到同样的错误。 url='https://www.cnn.com' r=se.get(url,proxies=proxy) 给我ProxyError: HTTPSConnectionPool(host='www.cnn.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', error(10053, 'An established connection was aborted by the software in your host machine')))
  • 你的requests_version怎么样,没有https代理可以访问网站吗?
  • 是的,我可以。 requests == 2.18.4。我刚刚升级到2.20.1。同样的错误。我正在运行 python 2.7.15
猜你喜欢
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
相关资源
最近更新 更多