2

API 応答を import.io からファイルまたはリストにロードするのに苦労しています。

私が使用しているエンドポイントは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))

これを行う際の問題は、data[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 の応答の経験がある人はいますか? 私が他のソースから行う他のすべてのjsonline読み取りは、これを除いて正常に機能します。

コメントに基づいて編集:

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'
4

1 に答える 1

5

二重エンコードしているコードにバグがあります。

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)
于 2016-11-29T19:39:31.660 に答える