使用Python从import.io加载时出现JSON Line问题

joh*_*855 2 python json import.io jsonlines

我很难尝试将import.io中的API响应加载到文件或列表中.

我正在使用的是 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))
Run Code Online (Sandbox Code Playgroud)

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

data[1] = IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

这是一个例子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'
Run Code Online (Sandbox Code Playgroud)

有没有人对此API的响应有经验?我从其他来源做的所有其他jsonline读取工作正常,除了这一个.

根据评论编辑:

print repr(open(temporary_json_file_path).read(300))
Run Code Online (Sandbox Code Playgroud)

给出这个:

'"{\\"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'
Run Code Online (Sandbox Code Playgroud)

小智 5

您在代码中遇到了双重编码的错误:

with open(temporary_json_file_path, 'w') as outfile:
    json.dump(r.content, outfile)
Run Code Online (Sandbox Code Playgroud)

尝试:

with open(temporary_json_file_path, 'w') as outfile:
    outfile.write(r.content)
Run Code Online (Sandbox Code Playgroud)

  • 我不确定我是怎么错过的.我删除了错误的答案. (2认同)