Goal: www.baidu.com
Source:
import requests url = 'http://www.baidu.com' header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' } free_proxy = { ##'http': '163.204.241.160:9999' 'http': '123.206.54.52:8118' } response = requests.get(url=url, headers=header, proxies=free_proxy) print(response.status_code)
Use proxy '163.204.241.160:9999' appears
ProxyError:
Traceback (most recent call last): File "D:\Software\python3.7.4\lib\site-packages\urllib3\connection.py", line 160, in _new_conn (self._dns_host, self.port), self.timeout, **extra_kw) File "D:\Software\python3.7.4\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection raise err File "D:\Software\python3.7.4\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\Software\python3.7.4\lib\site-packages\urllib3\connectionpool.py", line 603, in urlopen chunked=chunked) File "D:\Software\python3.7.4\lib\site-packages\urllib3\connectionpool.py", line 355, in _make_request conn.request(method, url, **httplib_request_kw) File "D:\Software\python3.7.4\lib\http\client.py", line 1244, in request self._send_request(method, url, body, headers, encode_chunked) File "D:\Software\python3.7.4\lib\http\client.py", line 1290, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "D:\Software\python3.7.4\lib\http\client.py", line 1239, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "D:\Software\python3.7.4\lib\http\client.py", line 1026, in _send_output self.send(msg) File "D:\Software\python3.7.4\lib\http\client.py", line 966, in send self.connect() File "D:\Software\python3.7.4\lib\site-packages\urllib3\connection.py", line 183, in connect conn = self._new_conn() File "D:\Software\python3.7.4\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 0x0000000003A6F048>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。 During handling of the above exception, another exception occurred: Traceback (most recent call last): During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:/Study/工作/待解决问题/requests/requests_proxy.py", line 15, in <module> response = requests.get(url=url, headers=header, proxies=free_proxy) File "D:\Software\python3.7.4\lib\site-packages\requests\api.py", line 75, in get return request('get', url, params=params, **kwargs) File "D:\Software\python3.7.4\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "D:\Software\python3.7.4\lib\site-packages\requests\sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "D:\Software\python3.7.4\lib\site-packages\requests\sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "D:\Software\python3.7.4\lib\site-packages\requests\adapters.py", line 510, in send raise ProxyError(e, request=request) requests.exceptions.ProxyError: HTTPConnectionPool(host='182.34.33.38', port=9999): Max retries exceeded with url: http://www.baidu.com/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003A6F048>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))
Use proxy '123.206.54.52:8118' appears
Cudakedetrrchodidagerror :
Traceback (most recent call last): File "D:\Software\python3.7.4\lib\site-packages\urllib3\response.py", line 397, in _error_catcher yield File "D:\Software\python3.7.4\lib\site-packages\urllib3\response.py", line 479, in read data = self._fp.read(amt) File "D:\Software\python3.7.4\lib\http\client.py", line 457, in read n = self.readinto(b) File "D:\Software\python3.7.4\lib\http\client.py", line 501, in readinto n = self.fp.readinto(b) File "D:\Software\python3.7.4\lib\socket.py", line 589, in readinto return self._sock.recv_into(b) ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\Software\python3.7.4\lib\site-packages\requests\models.py", line 750, in generate for chunk in self.raw.stream(chunk_size, decode_content=True): File "D:\Software\python3.7.4\lib\site-packages\urllib3\response.py", line 531, in stream data = self.read(amt=amt, decode_content=decode_content) File "D:\Software\python3.7.4\lib\site-packages\urllib3\response.py", line 496, in read raise IncompleteRead(self._fp_bytes_read, self.length_remaining) File "D:\Software\python3.7.4\lib\contextlib.py", line 130, in __exit__ self.gen.throw(type, value, traceback) File "D: \ Software \ python3.7.4 \ lib \ Site-Packages \ urllib3 \ response.py", Line 415, in _error_catcher The raise ProtocolError ( 'Broken Connection: R & lt%'% E, E) urllib3.exceptions.ProtocolError: ( "connection broken: ConnectionResetError (10054 , ' forcibly closed by the remote host an existing connection.', None, 10054, None )", ConnectionResetError (10054, ' forcibly closed by the remote host an existing connection.', None , 10054, None)) During Exception Handling of at The above, Another Exception occurred: Traceback (MOST recent Results Last Call): File "D: / Study / work / problem to be solved /requests/requests_proxy.py", line 16, in < Module> the Response = requests.get (url = url, headers = header, Proxies = free_proxy) File "D: \ Software \ python3.7.4 \ lib \ Site-Packages Standard Package \ Requests \ api.py", Line 75, in GET in get return request('get', url, params=params, **kwargs) File "D:\Software\python3.7.4\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "D:\Software\python3.7.4\lib\site-packages\requests\sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "D:\Software\python3.7.4\lib\site-packages\requests\sessions.py", line 686, in send r.content File "D:\Software\python3.7.4\lib\site-packages\requests\models.py", line 828, in content self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b'' File "D:\Software\python3.7.4\lib\site-packages\requests\models.py", line 753, in generate raise ChunkedEncodingError(e) requests.exceptions.ChunkedEncodingError: ( "Connection broken: ConnectionResetError (10054, '. forcibly closed by the remote host is connected to a conventional', None, 10054, None)", ConnectionResetError (10054, 'remote host forcibly closed by a conventional connection. ', None, 10054, None))
Finally, url = 'http://www.baidu.com' changed url = 'https://www.baidu.com', more than two proxy address tests are successful, the results: 200