[英]How to download csv files from a json file which contains the url to the csv files?
我正在运行一个文件,该文件将包含 url 的 json 文件下载到多个 csv 文件中。 它看起来像那样。
{"data_files":
{"i_p":["https://dashboard.s.com/api/1/user/downloads/2021-05-05/all_2021-05-05_i_p.1_0.1.csv.gz",
"https://dashboard.s.com/api/1/user/downloads/2021-05-05/all_2021-05-05_i_p.1_0.2.csv.gz"],
"c_p":["https://dashboard.s.com/api/1/user/downloads/2021-05-05/all_2021-05-05_c_p.1_0.1.csv.gz",
"https://dashboard.s.com/api/1/user/downloads/2021-05-05/all_2021-05-05_c_p.1_0.2.csv.gz"]},
"date":"2021-05-05"}
现在我想下载 json 文件中的所有文件。 我正在使用以下代码。
import requests
from requests.auth import HTTPBasicAuth
import json
import urllib
import datetime
myResponse1 = requests.get('https://dashboard.s.com/api/1/user.json?api_key=xxxxxxxxx&personal_key=xxxxxxxx')
if(myResponse1.ok):
file = open("V.json", "wb")
file.write(myResponse1.content)
file.close()
file = open('V.json', 'r')
id = 0
for line in file:
response = urllib.request.urlretrieve(line, str(id) + '.csv')
id+=1
else:
# If response code is not ok (200), print the resulting http error code with description
myResponse1.raise_for_status()
The above code is able to download the json file but I am not sure how to use the json file to download the csv files stored inside the json.Can someone tell me how can I do that?
不得不玩弄你的 json,但如果你能把它弄到迭代键/值对的地步,使用请求来下载文件
data = '''{"data_files":
{"i_p":["https://dashboard.s.com/api/1/user/downloads/2021-05-05/all_2021-05-05_i_p.1_0.1.csv.gz",
"https://dashboard.s.com/api/1/user/downloads/2021-05-05/all_2021-05-05_i_p.1_0.2.csv.gz"],
"c_p":["https://dashboard.s.com/api/1/user/downloads/2021-05-05/all_2021-05-05_c_p.1_0.1.csv.gz",
"https://dashboard.s.com/api/1/user/downloads/2021-05-05/all_2021-05-05_c_p.1_0.2.csv.gz"]},
"date":"2021-05-05"}'''
dataj = json.loads(data)
for k,v in dataj['data_files'].items():
for f in v:
print(f)
r = requests.get(f, allow_redirects=True)
print(f.split('/')[-1]) # split out the file name
open(f.split('/')[-1], 'wb').write(r.content) # write the file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.