从加载时出现JSON行问题导入.io使用Python

2024-04-23 21:46:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难从中加载API响应导入.io放入文件或列表中。你知道吗

我使用的enpoint是https://data.import.io/extractor/{0}/json/latest?_apikey={1}

以前我所有的脚本都设置为使用普通的JSON,而且都工作得很好,但现在他们决定使用JSON行,但不知怎么的,它似乎格式不正确。你知道吗

我尝试调整脚本的方法是按以下方式读取API响应:

url_call = 'https://data.import.io/extractor/{0}/json/latest?_apikey={1}'.format(extractors_row_dict['id'], auth_key)
r = requests.get(url_call)

with open(temporary_json_file_path, 'w') as outfile:
    json.dump(r.content, outfile)

data = []
with open(temporary_json_file_path) as f:
    for line in f:
        data.append(json.loads(line))

这样做的问题是,当我检查数据[0]时,所有json文件内容都被转储到其中。。。你知道吗

data[1] = IndexError: list index out of range

以下是data[0][:300]的示例:

u'{"url":"https://www.example.com/de/shop?condition[0]=new&page=1&lc=DE&l=de","result":{"extractorData":{"url":"https://www.example.com/de/shop?condition[0]=new&page=1&lc=DE&l=de","resourceId":"23455234","data":[{"group":[{"Brand":[{"text":"Brand","href":"https://www.example.com'

有人对这个API的响应有经验吗? 我从其他来源所做的所有其他在线阅读工作都很好,除了这个。你知道吗

根据评论编辑:

print repr(open(temporary_json_file_path).read(300))

给出:

'"{\\"url\\":\\"https://www.example.com/de/shop?condition[0]=new&page=1&lc=DE&l=de\\",\\"result\\":{\\"extractorData\\":{\\"url\\":\\"https://www.example.com/de/shop?condition[0]=new&page=1&lc=DE&l=de\\",\\"resourceId\\":\\"df8de15cede2e96fce5fe7e77180e848\\",\\"data\\":[{\\"group\\":[{\\"Brand\\":[{\\"text\\":\\"Bra'

Tags: httpscomapijsonurlnewdataexample
1条回答
网友
1楼 · 发布于 2024-04-23 21:46:19

您的代码中有一个错误,您正在进行双重编码:

with open(temporary_json_file_path, 'w') as outfile:
    json.dump(r.content, outfile)

尝试:

with open(temporary_json_file_path, 'w') as outfile:
    outfile.write(r.content)

相关问题 更多 >