API ConnectionRefusedError: [WinError 10061], 适用于 Jupyter 而不是 Pycharm

问题描述

我在运行直接从 nba_api 存储库提供的代码时遇到问题。我遇到的另一个奇怪问题是,当我在 Jupyter notebook 上运行代码时,我得到了一个类似的错误,但在下一个单元格中获得了我想要的输出

代码
'''

player_info = 
commonplayerinfo.CommonPlayerInfo(player_id=2544)

custom_headers = {

'Host': 'stats.nba.com','Connection': 'keep-alive','Cache-Control': 'max-age=0','Upgrade-Insecure-Requests': '1','User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 
 10_14_3) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/73.0.3683.86 Safari/537.36','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3','Accept-Encoding': 'gzip,deflate,br','Accept-Language': 'en-US,en;q=0.9',

}

 player_info = 
 commonplayerinfo.CommonPlayerInfo(player_id=2544,proxy='127.0.0.1:80',headers=custom_headers,timeout=100)

'''

当这段代码在 Jupyter 中运行时,我被提示一个错误,但当我运行 player_info.get_normalized_json() 时仍然能够得到我想要的输出

Jupyter 中的错误

'''ConnectionRefusedError                    Traceback (most recent call last)
~\anaconda3\lib\site-packages\urllib3\connection.py in _new_conn(self)
    158         try:
--> 159             conn = connection.create_connection(
    160                 (self._dns_host,self.port),self.timeout,**extra_kw

~\anaconda3\lib\site-packages\urllib3\util\connection.py in create_connection(address,timeout,source_address,socket_options)
     83     if err is not None:
---> 84         raise err
     85 

~\anaconda3\lib\site-packages\urllib3\util\connection.py in create_connection(address,socket_options)
     73                 sock.bind(source_address)
---> 74             sock.connect(sa)
     75             return sock

ConnectionRefusedError: [WinError 10061] No connection Could be made because the target machine actively refused it

During handling of the above exception,another exception occurred:

NewConnectionError                        Traceback (most recent call last)
~\anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self,method,url,body,headers,retries,redirect,assert_same_host,pool_timeout,release_conn,chunked,body_pos,**response_kw)
    666             if is_new_proxy_conn:
--> 667                 self._prepare_proxy(conn)
    668 

~\anaconda3\lib\site-packages\urllib3\connectionpool.py in _prepare_proxy(self,conn)
    931         conn.set_tunnel(self._proxy_host,self.port,self.proxy_headers)
--> 932         conn.connect()
    933 

~\anaconda3\lib\site-packages\urllib3\connection.py in connect(self)
    308         # Add certificate verification
--> 309         conn = self._new_conn()
    310         hostname = self.host

~\anaconda3\lib\site-packages\urllib3\connection.py in _new_conn(self)
    170         except SocketError as e:
--> 171             raise NewConnectionError(
    172                 self,"Failed to establish a new connection: %s" % e

NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x000001E16F3EF310>: Failed to establish a new connection: [WinError 10061] No connection Could be made because the target machine actively refused it

During handling of the above exception,another exception occurred:

MaxRetryError                             Traceback (most recent call last)
~\anaconda3\lib\site-packages\requests\adapters.py in send(self,request,stream,verify,cert,proxies)
    438             if not chunked:
--> 439                 resp = conn.urlopen(
    440                     method=request.method,~\anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self,**response_kw)
    725 
--> 726             retries = retries.increment(
    727                 method,error=e,_pool=self,_stacktrace=sys.exc_info()[2]

~\anaconda3\lib\site-packages\urllib3\util\retry.py in increment(self,response,error,_pool,_stacktrace)
    445         if new_retry.is_exhausted():
--> 446             raise MaxRetryError(_pool,error or ResponseError(cause))
    447 

MaxRetryError: HTTPSConnectionPool(host='stats.nba.com',port=443): Max retries exceeded with url: /stats/commonplayerinfo?LeagueID=&PlayerID=2544 (Caused by ProxyError('Cannot connect to proxy.',NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001E16F3EF310>: Failed to establish a new connection: [WinError 10061] No connection Could be made because the target machine actively refused it')))

During handling of the above exception,another exception occurred:

ProxyError                                Traceback (most recent call last)
<ipython-input-5-c0ff4e45a10b> in <module>
     10     'Accept-Language': 'en-US,11 }
---> 12 player_info = commonplayerinfo.CommonPlayerInfo(player_id=2544,timeout=100)

~\anaconda3\lib\site-packages\nba_api\stats\endpoints\commonplayerinfo.py in __init__(self,player_id,league_id_nullable,proxy,get_request)
     30         }
     31         if get_request:
---> 32             self.get_request()
     33 
     34     def get_request(self):

~\anaconda3\lib\site-packages\nba_api\stats\endpoints\commonplayerinfo.py in get_request(self)
     33 
     34     def get_request(self):
---> 35         self.nba_response = NBAStatsHTTP().send_api_request(
     36             endpoint=self.endpoint,37             parameters=self.parameters,~\anaconda3\lib\site-packages\nba_api\library\http.py in send_api_request(self,endpoint,parameters,referer,raise_exception_on_error)
    128 
    129         if not contents:
--> 130             response = requests.get(url=base_url,params=parameters,headers=request_headers,proxies=proxies,timeout=timeout)
    131             url = response.url
    132             status_code = response.status_code

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

~\anaconda3\lib\site-packages\requests\api.py in request(method,**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,data,cookies,files,auth,allow_redirects,proxies,hooks,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,**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,proxies)
    508 
    509             if isinstance(e.reason,_ProxyError):
--> 510                 raise ProxyError(e,request=request)
    511 
    512             if isinstance(e.reason,_SSLError):

ProxyError: HTTPSConnectionPool(host='stats.nba.com',NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001E16F3EF310>: Failed to establish a new connection: [WinError 10061] No connection Could be made because the target machine actively refused it')))

每当我在 Pycharm 中运行时,我都会遇到类似的错误并且无法运行 player_info.get_normalized_json(),因为代码在出现错误后无法执行

Pycharm 错误

'''

Traceback (most recent call last):
  File "C:\python39\urllib3\connection.py",line 169,in _new_conn
    conn = connection.create_connection(
  File "C:\python39\urllib3\util\connection.py",line 96,in create_connection
    raise err
  File "C:\python39\urllib3\util\connection.py",line 86,in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection Could be made because the target machine actively refused it

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\python39\urllib3\connectionpool.py",line 696,in urlopen
    self._prepare_proxy(conn)
  File "C:\python39\urllib3\connectionpool.py",line 964,in _prepare_proxy
    conn.connect()
  File "C:\python39\urllib3\connection.py",line 353,in connect
    conn = self._new_conn()
  File "C:\python39\urllib3\connection.py",line 181,in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x00000245F8DF75B0>: Failed to establish a new connection: [WinError 10061] No connection Could be made because the target machine actively refused it

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\python39\requests\adapters.py",line 439,in send
    resp = conn.urlopen(
  File "C:\python39\urllib3\connectionpool.py",line 755,in urlopen
    retries = retries.increment(
  File "C:\python39\urllib3\util\retry.py",line 574,in increment
    raise MaxRetryError(_pool,error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='stats.nba.com',NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000245F8DF75B0>: Failed to establish a new connection: [WinError 10061] No connection Could be made because the target machine actively refused it')))

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Robert\Desktop\Python Projects\NBAtrivia\main.py",line 46,in <module>
    getPlayerInfo(playerID)
  File "C:\Users\Robert\Desktop\Python Projects\NBAtrivia\main.py",line 33,in getPlayerInfo
    player_info = commonplayerinfo.CommonPlayerInfo(player_id=2544,proxy='http://127.0.0.1:80',timeout=100)
  File "C:\python39\nba_api\stats\endpoints\commonplayerinfo.py",line 32,in __init__
    self.get_request()
  File "C:\python39\nba_api\stats\endpoints\commonplayerinfo.py",line 35,in get_request
    self.nba_response = NBAStatsHTTP().send_api_request(
  File "C:\python39\nba_api\library\http.py",line 130,in send_api_request
    response = requests.get(url=base_url,timeout=timeout)
  File "C:\python39\requests\api.py",line 76,in get
    return request('get',**kwargs)
  File "C:\python39\requests\api.py",line 61,in request
    return session.request(method=method,**kwargs)
  File "C:\python39\requests\sessions.py",line 542,in request
    resp = self.send(prep,**send_kwargs)
  File "C:\python39\requests\sessions.py",line 655,in send
    r = adapter.send(request,**kwargs)
  File "C:\python39\requests\adapters.py",line 510,in send
    raise ProxyError(e,request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='stats.nba.com',NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000245F8DF75B0>: Failed to establish a new connection: [WinError 10061] No connection Could be made because the target machine actively refused it')))

任何帮助表示赞赏。谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)