首页 > 解决方案 > 来自 NOAA API 的天气数据从服务器返回错误

问题描述

我已经多次运行此代码,目的是从列出的气象站返回 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, headers, encode_chunked)
   1254         """Send a complete request to the server."""
-> 1255         self._send_request(method, url, body, headers, encode_chunked)
   1256 

~\Anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers, 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, message_body, 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, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, 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,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
----> 2 df_d = pd.read_json(d)
      3 df = df_d.fillna(0) #replaces NaN values with 0
      4 
      5 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)

~\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, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows, 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, encoding, compression, 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, encoding, compression, mode, 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, data, timeout)
    223 
    224 def install_opener(opener):

~\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
    523 
    524         sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 525         response = self._open(req, data)
    526 
    527         # post-process response

~\Anaconda3\lib\urllib\request.py in _open(self, req, 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, req,
   1398                 context=self._context, check_hostname=self._check_hostname)
   1399 

~\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **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>
```

标签: pythondataframeapiurlnoaa

解决方案


推荐阅读