仅对一个特定网站请求错误:OS 错误 10054 连接中止

Requests error only with one specific website : OS Error 10054 Connection aborted

我只在这个特定网站上收到此错误。

我用谷歌搜索并阅读了所有内容,但 none 解决了我的问题。

非常简单的一段代码,有人能弄清楚这是怎么回事吗?其他网站运行良好。

import requests

url = 'http://www.trading212.com/en/Trading-Instruments?id=3'

response = requests.get(url)

错误代码:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    671                 headers=headers,
--> 672                 chunked=chunked,
    673             )

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    420                     # Otherwise it looks like a bug in the code.
--> 421                     six.raise_from(e, None)
    422         except (SocketTimeout, BaseSSLError, SocketError) as e:

~\Anaconda3\lib\site-packages\urllib3\packages\six.py in raise_from(value, from_value)

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    415                 try:
--> 416                     httplib_response = conn.getresponse()
    417                 except BaseException as e:

~\Anaconda3\lib\http\client.py in getresponse(self)
   1320             try:
-> 1321                 response.begin()
   1322             except ConnectionError:

~\Anaconda3\lib\http\client.py in begin(self)
    295         while True:
--> 296             version, status, reason = self._read_status()
    297             if status != CONTINUE:

~\Anaconda3\lib\http\client.py in _read_status(self)
    256     def _read_status(self):
--> 257         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    258         if len(line) > _MAXLINE:

~\Anaconda3\lib\socket.py in readinto(self, b)
    588             try:
--> 589                 return self._sock.recv_into(b)
    590             except timeout:

~\Anaconda3\lib\site-packages\urllib3\contrib\pyopenssl.py in recv_into(self, *args, **kwargs)
    317             else:
--> 318                 raise SocketError(str(e))
    319         except OpenSSL.SSL.ZeroReturnError:

OSError: (10054, 'WSAECONNRESET')

During handling of the above exception, another exception occurred:

ProtocolError                             Traceback (most recent call last)
~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    448                     retries=self.max_retries,
--> 449                     timeout=timeout
    450                 )

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    719             retries = retries.increment(
--> 720                 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
    721             )

~\Anaconda3\lib\site-packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    399             if read is False or not self._is_method_retryable(method):
--> 400                 raise six.reraise(type(error), error, _stacktrace)
    401             elif read is not None:

~\Anaconda3\lib\site-packages\urllib3\packages\six.py in reraise(tp, value, tb)
    733             if value.__traceback__ is not tb:
--> 734                 raise value.with_traceback(tb)
    735             raise value

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    671                 headers=headers,
--> 672                 chunked=chunked,
    673             )

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    420                     # Otherwise it looks like a bug in the code.
--> 421                     six.raise_from(e, None)
    422         except (SocketTimeout, BaseSSLError, SocketError) as e:

~\Anaconda3\lib\site-packages\urllib3\packages\six.py in raise_from(value, from_value)

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    415                 try:
--> 416                     httplib_response = conn.getresponse()
    417                 except BaseException as e:

~\Anaconda3\lib\http\client.py in getresponse(self)
   1320             try:
-> 1321                 response.begin()
   1322             except ConnectionError:

~\Anaconda3\lib\http\client.py in begin(self)
    295         while True:
--> 296             version, status, reason = self._read_status()
    297             if status != CONTINUE:

~\Anaconda3\lib\http\client.py in _read_status(self)
    256     def _read_status(self):
--> 257         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    258         if len(line) > _MAXLINE:

~\Anaconda3\lib\socket.py in readinto(self, b)
    588             try:
--> 589                 return self._sock.recv_into(b)
    590             except timeout:

~\Anaconda3\lib\site-packages\urllib3\contrib\pyopenssl.py in recv_into(self, *args, **kwargs)
    317             else:
--> 318                 raise SocketError(str(e))
    319         except OpenSSL.SSL.ZeroReturnError:

ProtocolError: ('Connection aborted.', OSError("(10054, 'WSAECONNRESET')"))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
<ipython-input-2-c162877d446b> in <module>
      1 url = 'http://www.trading212.com/en/Trading-Instruments?id=3'
----> 2 response = requests.get(url)

~\Anaconda3\lib\site-packages\requests\api.py in get(url, params, **kwargs)
     74 
     75     kwargs.setdefault('allow_redirects', True)
---> 76     return request('get', url, params=params, **kwargs)
     77 
     78 

~\Anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
     59     # cases, and look like a memory leak in others.
     60     with sessions.Session() as session:
---> 61         return session.request(method=method, url=url, **kwargs)
     62 
     63 

~\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    528         }
    529         send_kwargs.update(settings)
--> 530         resp = self.send(prep, **send_kwargs)
    531 
    532         return resp

~\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
    663 
    664         # Resolve redirects if allowed.
--> 665         history = [resp for resp in gen] if allow_redirects else []
    666 
    667         # Shuffle things around if there's history.

~\Anaconda3\lib\site-packages\requests\sessions.py in <listcomp>(.0)
    663 
    664         # Resolve redirects if allowed.
--> 665         history = [resp for resp in gen] if allow_redirects else []
    666 
    667         # Shuffle things around if there's history.

~\Anaconda3\lib\site-packages\requests\sessions.py in resolve_redirects(self, resp, req, stream, timeout, verify, cert, proxies, yield_requests, **adapter_kwargs)
    243                     proxies=proxies,
    244                     allow_redirects=False,
--> 245                     **adapter_kwargs
    246                 )
    247 

~\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
    641 
    642         # Send the request
--> 643         r = adapter.send(request, **kwargs)
    644 
    645         # Total elapsed time of the request (approximately)

~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    496 
    497         except (ProtocolError, socket.error) as err:
--> 498             raise ConnectionError(err, request=request)
    499 
    500         except MaxRetryError as e:

ConnectionError: ('Connection aborted.', OSError("(10054, 'WSAECONNRESET')"))

我猜他们屏蔽了 "unwanted" 用户。恶搞User-Agent头,应该没问题:

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15'}
response = requests.get(url, headers=headers)