Вы можете сделать что-то вроде этого, предполагая, что у вас есть загруженный кадр данных.
import requests
from os.path import join
for index, row in df.iterrows():
url = row['url']
file_name = url.split('/')[-1]
r = requests.get(url)
abs_file_name = join(row['city'],row['buildingName']+str(row['buildingId']),row['imgType'],file_name)
if r.status_code == 200:
with open(abs_file_name, "wb") as f:
f.write(r.content)
Отредактированный код:
import requests
from os.path import join,expanduser
import os
home = expanduser("~")
df = pd.DataFrame()
# df.append({})
for index, row in df.iterrows():
url = row['url']
file_name = url.split('/')[-1]
r = requests.get(url)
filepath = join(home,row['city'],row['buildingName']+str(row['buildingId']),row['imgType'])
if not os.path.exists(filepath):
os.makedirs(filepath)
filepath = join(filepath, file_name)
# print(filepath)
if r.status_code == 200:
with open(filepath, "wb") as f:
f.write(r.content)