问题描述
我已多次运行此代码,目的是从列出的气象站返回 NOAA 数据。该代码以前工作过,但今天它返回以下错误。我不知道为什么考虑到代码没有改变,谁能帮我创建一个解决方法?我正在使用 jupyter 笔记本。
d = "https://www.ncei.noaa.gov/access/services/data/v1?dataset=daily-summaries&dataTypes=PRCP,SNow,TMAX,TMIN&stations=USW00026492,USW00024037,USW00014916,USW00014913,USW00094847,USW00014607,USW00054773,USW00014742,USW00094700,USW00014820,USW00014860,USW00023199,USW00023188,USW00023050,USW00003125,USW00023160,USW00022004,USW00022010,USW00012907,USW00012912,USW00012923,USW00012924,USW00012916,USW00012839,USW00012842,USW00022521,RQW00011630,USW00013743&startDate=2020-02-01&endDate=2020-02-01&includeAttributes=0&includeStationName=true&units=standard&format=json" #request from NOAA API
df_d = pd.read_json(d)
df = df_d.fillna(0) #replaces NaN values with 0
df.insert(3,"Lat",[60.785,46.427,47.943,46.837,42.231,46.871,43.35,44.468,44.576,41.406,42.08,32.817,32.734,35.042,32.833,32.131,29.378,27.533,28.783,29.273,27.774,29.997,25.791,27.962,21.324,18.255,38.847],True)
df.insert(4,"Lon",[-148.839,-105.883,-97.184,-92.183,-83.331,-68.017,-76.385,-73.15,-71.179,-81.852,-80.182,-115.683,-117.183,-106.616,-114.4,-110.955,-100.927,-99.467,-97.083,-94.859,-97.512,-90.278,-80.316,-82.54,-157.929,-65.641,-77.035],True)
df```
```
ConnectionResetError Traceback (most recent call last)
~\Anaconda3\lib\urllib\request.py in do_open(self,http_class,req,**http_conn_args)
1353 try:
-> 1354 h.request(req.get_method(),req.selector,req.data,headers,1355 encode_chunked=req.has_header('transfer-encoding'))
~\Anaconda3\lib\http\client.py in request(self,method,url,body,encode_chunked)
1254 """Send a complete request to the server."""
-> 1255 self._send_request(method,encode_chunked)
1256
~\Anaconda3\lib\http\client.py in _send_request(self,encode_chunked)
1300 body = _encode(body,'body')
-> 1301 self.endheaders(body,encode_chunked=encode_chunked)
1302
~\Anaconda3\lib\http\client.py in endheaders(self,message_body,encode_chunked)
1249 raise CannotSendHeader()
-> 1250 self._send_output(message_body,encode_chunked=encode_chunked)
1251
~\Anaconda3\lib\http\client.py in _send_output(self,encode_chunked)
1009 del self._buffer[:]
-> 1010 self.send(msg)
1011
~\Anaconda3\lib\http\client.py in send(self,data)
949 if self.auto_open:
--> 950 self.connect()
951 else:
~\Anaconda3\lib\http\client.py in connect(self)
1423
-> 1424 self.sock = self._context.wrap_socket(self.sock,1425 server_hostname=server_hostname)
~\Anaconda3\lib\ssl.py in wrap_socket(self,sock,server_side,do_handshake_on_connect,suppress_ragged_eofs,server_hostname,session)
499 # ctx._wrap_socket()
--> 500 return self.sslsocket_class._create(
501 sock=sock,~\Anaconda3\lib\ssl.py in _create(cls,context,session)
1039 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
-> 1040 self.do_handshake()
1041 except (OSError,ValueError):
~\Anaconda3\lib\ssl.py in do_handshake(self,block)
1308 self.settimeout(None)
-> 1309 self._sslobj.do_handshake()
1310 finally:
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception,another exception occurred:
URLError Traceback (most recent call last)
<ipython-input-2-75e4cdb60933> in <module>
1 d = "https://www.ncei.noaa.gov/access/services/data/v1?dataset=daily-summaries&dataTypes=PRCP,USW00013743&startDate=2020-02-01&endDate=2020-02-01&includeAttributes=0&includeStationName=true&units=standard&format=json" #request from NOAA API
----> 2 df_d = pd.read_json(d)
3 df = df_d.fillna(0) #replaces NaN values with 0
4
5 df.insert(3,True)
~\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args,**kwargs)
197 else:
198 kwargs[new_arg_name] = new_arg_value
--> 199 return func(*args,**kwargs)
200
201 return cast(F,wrapper)
~\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args,**kwargs)
297 )
298 warnings.warn(msg,FutureWarning,stacklevel=stacklevel)
--> 299 return func(*args,**kwargs)
300
301 return wrapper
~\Anaconda3\lib\site-packages\pandas\io\json\_json.py in read_json(path_or_buf,orient,typ,dtype,convert_axes,convert_dates,keep_default_dates,numpy,precise_float,date_unit,encoding,lines,chunksize,compression,nrows,storage_options)
538 convert_axes = True
539
--> 540 json_reader = JsonReader(
541 path_or_buf,542 orient=orient,~\Anaconda3\lib\site-packages\pandas\io\json\_json.py in __init__(self,filepath_or_buffer,storage_options)
620 raise ValueError("nrows can only be passed if lines=True")
621
--> 622 data = self._get_data_from_filepath(filepath_or_buffer)
623 self.data = self._preprocess_data(data)
624
~\Anaconda3\lib\site-packages\pandas\io\json\_json.py in _get_data_from_filepath(self,filepath_or_buffer)
657 or file_exists(filepath_or_buffer)
658 ):
--> 659 self.handles = get_handle(
660 filepath_or_buffer,661 "r",~\Anaconda3\lib\site-packages\pandas\io\common.py in get_handle(path_or_buf,mode,memory_map,is_text,errors,storage_options)
556
557 # open URLs
--> 558 ioargs = _get_filepath_or_buffer(
559 path_or_buf,560 encoding=encoding,~\Anaconda3\lib\site-packages\pandas\io\common.py in _get_filepath_or_buffer(filepath_or_buffer,storage_options)
287 "storage_options passed with file object or non-fsspec file path"
288 )
--> 289 req = urlopen(filepath_or_buffer)
290 content_encoding = req.headers.get("content-encoding",None)
291 if content_encoding == "gzip":
~\Anaconda3\lib\site-packages\pandas\io\common.py in urlopen(*args,**kwargs)
193 import urllib.request
194
--> 195 return urllib.request.urlopen(*args,**kwargs)
196
197
~\Anaconda3\lib\urllib\request.py in urlopen(url,data,timeout,cafile,capath,cadefault,context)
220 else:
221 opener = _opener
--> 222 return opener.open(url,timeout)
223
224 def install_opener(opener):
~\Anaconda3\lib\urllib\request.py in open(self,fullurl,timeout)
523
524 sys.audit('urllib.Request',req.full_url,req.headers,req.get_method())
--> 525 response = self._open(req,data)
526
527 # post-process response
~\Anaconda3\lib\urllib\request.py in _open(self,data)
540
541 protocol = req.type
--> 542 result = self._call_chain(self.handle_open,protocol,protocol +
543 '_open',req)
544 if result:
~\Anaconda3\lib\urllib\request.py in _call_chain(self,chain,kind,meth_name,*args)
500 for handler in handlers:
501 func = getattr(handler,meth_name)
--> 502 result = func(*args)
503 if result is not None:
504 return result
~\Anaconda3\lib\urllib\request.py in https_open(self,req)
1395
1396 def https_open(self,req):
-> 1397 return self.do_open(http.client.HTTPSConnection,1398 context=self._context,check_hostname=self._check_hostname)
1399
~\Anaconda3\lib\urllib\request.py in do_open(self,**http_conn_args)
1355 encode_chunked=req.has_header('transfer-encoding'))
1356 except OSError as err: # timeout error
-> 1357 raise URLError(err)
1358 r = h.getresponse()
1359 except:
URLError: <urlopen error [WinError 10054] An existing connection was forcibly closed by the remote host>
```
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)