目標:www.baidu.comhtml
源碼:python
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類型地址 ##'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)
使用代理'163.204.241.160:9999'出現api
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): File "D:\Software\python3.7.4\lib\site-packages\requests\adapters.py", line 449, in send timeout=timeout File "D:\Software\python3.7.4\lib\site-packages\urllib3\connectionpool.py", line 641, in urlopen _stacktrace=sys.exc_info()[2]) File "D:\Software\python3.7.4\lib\site-packages\urllib3\util\retry.py", line 399, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: 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] 因爲目標計算機積極拒絕,沒法鏈接。'))) 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] 因爲目標計算機積極拒絕,沒法鏈接。')))
查過本身電腦網絡配置中沒有設置代理。session
使用代理'123.206.54.52:8118'出現socket
ChunkedEncodingError:ide
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 raise ProtocolError('Connection broken: %r' % e, e) urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionResetError(10054, '遠程主機強迫關閉了一個現有的鏈接。', None, 10054, None)", ConnectionResetError(10054, '遠程主機強迫關閉了一個現有的鏈接。', None, 10054, None)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:/Study/工做/待解決問題/requests/requests_proxy.py", line 16, 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 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, '遠程主機強迫關閉了一個現有的鏈接。', None, 10054, None)", ConnectionResetError(10054, '遠程主機強迫關閉了一個現有的鏈接。', None, 10054, None))
推薦閱讀:https://blog.csdn.net/wangzuxi/article/details/40377467 測試
最後,將url = 'http://www.baidu.com' 改爲 url = 'https://www.baidu.com', 使用以上兩個代理地址測試都能成功,結果:200url
-----.net
換了一個新的代理: 'http': '47.93.36.195:8118'
url = 'http://www.baidu.com'
屢次運行,返回狀態有200,503,還有ChunkedEncodingError
因而可知,ChunkedEncodingError錯誤並非代理問題,爲穩定,仍是將url改成https爲好。