[英]I'm trying to stream data from alpaca API. Using json.dumps gives me error code: Object of type set is not JSON serializable
[英]I'm trying to learn to to request, load and parse Json data from API. I'm getting a value error
在解析和写入 CSV 之前,我一直在尝试学习从 API 检索 Json 数据。 我尝试了多种方法,但是我一直卡在第一个障碍上。 使用下面的代码,我在尝试从 URL 检索数据时收到预期值错误。 我错过了什么?
代码
import requests
import pandas as pd
import json
import pprint
import seaborn as sns
import matplotlib.pyplot as plt
base_url="https://data.sec.gov/api/xbrl/companyfacts/CIK0001627475.json"
first_response=requests.get(base_url)
response_list=first_response.json()
错误信息
JSONDecodeError Traceback (most recent call last)
<ipython-input-7-10ae360550df> in <module>
1 base_url="https://data.sec.gov/api/xbrl/companyfacts/CIK0001627475.json"
2 first_response=requests.get(base_url)
----> 3 response_list=first_response.json()
~\Anaconda3\lib\site-packages\requests\models.py in json(self, **kwargs)
898 # used.
899 pass
--> 900 return complexjson.loads(self.text, **kwargs)
901
902 @property
~\Anaconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
355 parse_int is None and parse_float is None and
356 parse_constant is None and object_pairs_hook is None and not kw):
--> 357 return _default_decoder.decode(s)
358 if cls is None:
359 cls = JSONDecoder
~\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
~\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
有些请求成功,有些则不成功,因此您可以尝试:
import requests
import json
base_url = "https://data.sec.gov/api/xbrl/companyfacts/CIK0001627475.json"
succeed = False
while not succeed:
response = requests.get(base_url)
try:
dict_response = json.loads(response.text)
succeed = True
except:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.