使用请求学习代理的工作方式

我试图了解代理的工作原理,我做了一些修改,并且我理解代理(在这种情况下)用于隐藏您的真实IP。

我正在尝试一些东西,然后尝试简单地提出一个请求:

r = requests.get('http://info.cern.ch/hypertext/WWW/TheProject.html').text

在有史以来最简单,最古老的网站上。很好 然后,我尝试使用在网站上找到的代理提出相同的请求(该简单站点生成带有很多代理的txt文件)。

pr = '205.27.102.21:8080'
r = requests.get('http://info.cern.ch/hypertext/WWW/TheProject.html', proxies = {"http":pr,"https":pr}).text

但发生错误:

    Traceback (most recent call last):
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\connection.py", line 157, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] Impossibile stabilire la connessione. Risposta non corretta della parte connessa dopo l'intervallo di tempo oppure mancata risposta dall'host collegato
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\connectionpool.py", line 672, in urlopen
    chunked=chunked,
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Program Files\Python37\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Program Files\Python37\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Program Files\Python37\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Program Files\Python37\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "C:\Program Files\Python37\lib\http\client.py", line 956, in send
    self.connect()
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\connection.py", line 184, in connect
    conn = self._new_conn()
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000017E9D29A0B8>: Failed to establish a new connection: [WinError 10060] Impossibile stabilire la connessione. Risposta non corretta della parte connessa dopo l'intervallo di tempo oppure mancata risposta dall'host collegato
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\connectionpool.py", line 720, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\urllib3\util\retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='205.27.102.21', port=8080): Max retries exceeded with url: http://info.cern.ch/hypertext/WWW/TheProject.html (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError("<urllib3.connection.HTTPConnection object at 0x0000017E9D29A0B8>: Failed to establish a new connection: [WinError 10060] Impossibile stabilire la connessione. Risposta non corretta della parte connessa dopo l'intervallo di tempo oppure mancata risposta dall'host collegato")))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-9-cc664ac4f242>", line 1, in <module>
    r = requests.get('http://info.cern.ch/hypertext/WWW/TheProject.html', proxies = {"http":pr,"https":pr}).text
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Marco\PycharmProjects\ortuproject\venv\lib\site-packages\requests\adapters.py", line 510, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='205.27.102.21', port=8080): Max retries exceeded with url: http://info.cern.ch/hypertext/WWW/TheProject.html (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError("<urllib3.connection.HTTPConnection object at 0x0000017E9D29A0B8>: Failed to establish a new connection: [WinError 10060] Impossibile stabilire la connessione. Risposta non corretta della parte connessa dopo l'intervallo di tempo oppure mancata risposta dall'host collegato")))

我认为这是单个代理的问题,因此我尝试创建一个包含所有代理的池:

from itertools import cycle
pool = cycle(proxies)

for i in range(0,len(proxies)):
    pr = next(pool)
    try:
        r = requests.get('http://info.cern.ch/hypertext/WWW/TheProject.html', proxies={"http": pr, "https": pr}).text
    except Exception as e:
        print(e)

但是每次迭代都花了很多时间(很奇怪,因为没有代理,请求是立即的),而且我总是会出错。

那么,我做错了还是我误解了代理的工作方式?任何指导/解释将不胜感激!谢谢!

评论