如何在Python3中正确使用代理?

时间:2019-06-16 20:29:33

标签: python-3.x proxy python-requests

在尝试使用代码查找代理时:

proxy_list = open(dir_path + "/proxies.txt", 'r+').readlines()
proxy_list = [item.replace("""\n""", "") for item in proxy_list]
proxy_pool = cycle(proxy_list)

url = "https://httpbin.org/ip"
for i in range(len(proxy_list)-1):
    #Get a proxy from the pool
    proxy = next(proxy_pool)
    try:
        response = requests.get(url,proxies={"http": proxy, "https": proxy})
        working_proxies.append(proxy)
        break
    except:
        pass;

这会浏览我自己创建的代理列表,并一直持续到找到一个代理为止,这很好用,但是当我尝试使用请求连接到网站时,出现错误。

代码是:

l = requests.Session()
l.proxies = proxies

print("Checking Connection")
r = l.get("https://httpbin.org/ip")
print("your connecting through: ", r.text)

上面代码中的错误是:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 594, in urlopen
    self._prepare_proxy(conn)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy
    conn.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connection.py", line 308, in connect
    self._tunnel()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 906, in _tunnel
    (version, code, message) = response._read_status()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 265, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

所以我想知道的是,是否有更好的方法来处理python中的代理并确保它们正常工作?

0 个答案:

没有答案