Python Requests:proxy代理错误

Python Requests:proxy代理错误

 

源码:

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'出现

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] 由于目标计算机积极拒绝,无法连接。')))
 

查过自己电脑网络配置中没有设置代理。

 

 

使用代理'123.206.54.52:8118'出现

ChunkedEncodingError

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))
 

最后,将url = 'http://www.baidu.com' 改成 url = 'https://www.baidu.com', 使用以上两个代理地址测试都能成功,结果:200

 

换了一个新的代理: 'http': '47.93.36.195:8118' 

url = 'http://www.baidu.com' 

多次运行,返回状态有200,503,还有ChunkedEncodingError

由此可见,ChunkedEncodingError错误并不是代理问题,为稳定,还是将url改为https为好。

你可能感兴趣的:(python,爬虫)