简体   繁体   English

我正在尝试学习从 API 请求、加载和解析 Json 数据。 我收到一个值错误

[英]I'm trying to learn to to request, load and parse Json data from API. I'm getting a value error

I've been trying to learn to retrieve Json data from an API, before parsing and writing to CSV.在解析和写入 CSV 之前,我一直在尝试学习从 API 检索 Json 数据。 I've tried a number of methods, however I keep getting stuck at the first hurdle.我尝试了多种方法,但是我一直卡在第一个障碍上。 With the code below, I get an expecting value error when trying to retrieve the data from URL.使用下面的代码,我在尝试从 URL 检索数据时收到预期值错误。 What am I missing?我错过了什么?

Code代码

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()

Error message错误信息

        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)

Some requests are successful and others are not, so you can try:有些请求成功,有些则不成功,因此您可以尝试:

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.

相关问题 我正在尝试从 alpaca API 流式传输数据。 使用 json.dumps 给了我错误代码:类型集的对象不是 JSON 可序列化的 - I'm trying to stream data from alpaca API. Using json.dumps gives me error code: Object of type set is not JSON serializable 我正在尝试从 Airtable API 解析 Json 文件,但它没有解析空字段或分隔符 - I'm trying to parse a Json file from Airtable API, but it's not parsing empty fields or delimiters 我正在尝试使用 CSV 文件中的数据对饼图进行 plot 饼图,但出现错误,我不明白 - I'm trying to plot a pie chart using data from a CSV file but I'm getting an error I don't understand 我正在尝试使用 keras 库训练模型,但出现值错误 - I'm trying to train the model using keras library but I'm getting value error 我在尝试从网站抓取数据时遇到 KeyError - I'm getting KeyError trying to scrape data from website 我在尝试从urlresolver导入保留时遇到错误 - I'm getting error when trying to import reserve from urlresolver 尝试使用urllib.request从网站下载图像,但出现403错误。 如何更改用户代理? - Trying to download images from website with urllib.request but I'm getting a 403 error. How do I change the user agent? 我正在尝试使用 Python/Django 正确解析 JSON - I'm trying to parse a JSON correctly with Python/Django 尝试运行 mlagents-learn 命令时出现此错误。 正确软件包的所有正确版本都安装在 venv 中 - I'm getting this error when trying to run the mlagents-learn command. All the correct versions of the correct packages are installed in a venv 从Imgur api获取数据时出错403。 我哪里错了? - 403 error while getting data from Imgur api. Where am I going wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM