Python 代理错误:BeautifulSoup HTTPSConnectionPool

Python 代理错误:BeautifulSoup HTTPSConnectionPool,python,web-scraping,beautifulsoup,proxy,python-requests,Python,Web Scraping,Beautifulsoup,Proxy,Python Requests,假设我的代码调用从提供免费代理列表的网站抓取的列表中选择的随机代理来获取我的原始IP。 但是,当我运行下面的代码时,有时它会返回正确的响应(200和正确的响应),有时它会返回: MaxRetryError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by ProxyError('Cannot connect to proxy.', NewConnec

假设我的代码调用从提供免费代理列表的网站抓取的列表中选择的随机代理来获取我的原始IP。 但是,当我运行下面的代码时,有时它会返回正确的响应(200和正确的响应),有时它会返回:

MaxRetryError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by ProxyError('Cannot connect to proxy.',  NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000001EF83500DC8>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond')))

Traceback (most recent call last):

  File "<ipython-input-196-baf92a94e8ec>", line 19, in <module>
    response = s.get(url,proxies=proxyDict)

这个错误意味着什么?我有办法解决这个问题吗

尝试以下解决方案。它将继续尝试使用不同的代理,直到找到一个可用的代理。一旦找到一个工作的代理,脚本应该给您所需的响应并中断循环

import random
import requests
from bs4 import BeautifulSoup

url = 'https://httpbin.org/ip'

proxies = []

res = requests.get('https://free-proxy-list.net/', headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(res.text,"lxml")
for items in soup.select("#proxylisttable tbody tr"):
    proxy_list = ':'.join([item.text for item in items.select("td")[:2]])
    proxies.append(proxy_list)

while True:
    choosenProxy = random.choice(proxies)
    proxyDict = {
        'http' : f'http://{choosenProxy}',
        'https' : f'https://{choosenProxy}'
    }
    print("trying with:",proxyDict)
    try:
        response = requests.get(url,proxies=proxyDict,timeout=5)
        print(response.text)
        break
    except Exception:
        continue

“无法连接到代理。”是一条非常明确的错误消息。
import random
import requests
from bs4 import BeautifulSoup

url = 'https://httpbin.org/ip'

proxies = []

res = requests.get('https://free-proxy-list.net/', headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(res.text,"lxml")
for items in soup.select("#proxylisttable tbody tr"):
    proxy_list = ':'.join([item.text for item in items.select("td")[:2]])
    proxies.append(proxy_list)

while True:
    choosenProxy = random.choice(proxies)
    proxyDict = {
        'http' : f'http://{choosenProxy}',
        'https' : f'https://{choosenProxy}'
    }
    print("trying with:",proxyDict)
    try:
        response = requests.get(url,proxies=proxyDict,timeout=5)
        print(response.text)
        break
    except Exception:
        continue