Python: 使用代理服务器和requests库实现HTTPS请求

4

我正在使用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和requests 2.20.1。非代理使用requests可以使用,如下所示:
import requests
url='https://www.cnn.com'
r=requests.get(url)

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

我也尝试使用Python 3.6和Requests 2.20.1,但结果相同。


你添加了端口吗?{"https":"ip:port"}。或者尝试 {"https":"183.88.219.163:57457"} - KC.
我认为你没必要这么做,因为没有指定端口。 - jason
如果我的代理服务器对您有效,那么这意味着问题出现在您的代理服务器上。 - KC.
@kcorlidy。感谢您提供的代理。看起来它也不起作用。ProxyError: HTTPSConnectionPool(host='www.cnn.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003CD9208>: Failed to establish a new connection: [Errno 10061] No connection could be made because the target machine actively refused it',))) - jason
是的,不是所有的节点都对我有效。但你也可以在https://free-proxy-list.net/上找到一个。如果你还是出错了,请告诉我你的平台、Python版本和requests版本,这样我就能得到与你相同的错误,然后找到解决方案。 - KC.
显示剩余3条评论
1个回答

2

在请求中,它基于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。 - KC.
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'))). - jason
你的requests_version是多少?你能在没有https代理的情况下访问网站吗? - KC.
可以的。requests == 2.18.4。我刚刚升级到2.20.1。同样的错误。我正在运行Python 2.7.15。 - jason

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接