#python #dataframe #api #url #noaa
Вопрос:
Я несколько раз запускал этот код с целью возврата данных NOAA с перечисленных метеостанций. Код работал ранее, за исключением сегодняшнего дня, он возвращает ошибку ниже. Я не уверен, почему, учитывая, что код не изменился, может ли кто-нибудь помочь мне создать обходной путь? Я использую записную книжку jupyter.
d = "https://www.ncei.noaa.gov/access/services/data/v1?dataset=daily-summariesamp;dataTypes=PRCP,SNOW,TMAX,TMINamp;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,USW00013743amp;startDate=2020-02-01amp;endDate=2020-02-01amp;includeAttributes=0amp;includeStationName=trueamp;units=standardamp;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)
~Anaconda3liburllibrequest.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'))
~Anaconda3libhttpclient.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
~Anaconda3libhttpclient.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
~Anaconda3libhttpclient.py in endheaders(self, message_body, encode_chunked)
1249 raise CannotSendHeader()
-> 1250 self._send_output(message_body, encode_chunked=encode_chunked)
1251
~Anaconda3libhttpclient.py in _send_output(self, message_body, encode_chunked)
1009 del self._buffer[:]
-> 1010 self.send(msg)
1011
~Anaconda3libhttpclient.py in send(self, data)
949 if self.auto_open:
--> 950 self.connect()
951 else:
~Anaconda3libhttpclient.py in connect(self)
1423
-> 1424 self.sock = self._context.wrap_socket(self.sock,
1425 server_hostname=server_hostname)
~Anaconda3libssl.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,
~Anaconda3libssl.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):
~Anaconda3libssl.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-summariesamp;dataTypes=PRCP,SNOW,TMAX,TMINamp;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,USW00013743amp;startDate=2020-02-01amp;endDate=2020-02-01amp;includeAttributes=0amp;includeStationName=trueamp;units=standardamp;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)
~Anaconda3libsite-packagespandasutil_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)
~Anaconda3libsite-packagespandasutil_decorators.py in wrapper(*args, **kwargs)
297 )
298 warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
--> 299 return func(*args, **kwargs)
300
301 return wrapper
~Anaconda3libsite-packagespandasiojson_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,
~Anaconda3libsite-packagespandasiojson_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
~Anaconda3libsite-packagespandasiojson_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",
~Anaconda3libsite-packagespandasiocommon.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,
~Anaconda3libsite-packagespandasiocommon.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":
~Anaconda3libsite-packagespandasiocommon.py in urlopen(*args, **kwargs)
193 import urllib.request
194
--> 195 return urllib.request.urlopen(*args, **kwargs)
196
197
~Anaconda3liburllibrequest.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):
~Anaconda3liburllibrequest.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
~Anaconda3liburllibrequest.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:
~Anaconda3liburllibrequest.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
~Anaconda3liburllibrequest.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
~Anaconda3liburllibrequest.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>
```