[英]Python: HTTPS using a proxy server with requests
我正在使用 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)
唯一的區別是http
與https
這是回溯:
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]>
>>>
還嘗試了 python 3.6 請求 2.20.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.