我在python和编码方面很新;
我有一个数据,如下所示;
Color Gender Model link
Black Man Sneakers https://....
Black Man Boots https://....
White Woman Sneakers https://....
Brown Woman Sneakers https://....
Black Man Sneakers https://....
White Woman Boots https://....我想下载这些图片链接,并将它们保存在一个基于颜色-性别-模型组合的目录中。
最后,我需要Black_Man_Sneakers文件夹,所有相关的图像(在本例中,第一和第六链接)都应该在该文件夹中。
我该怎么开始?任何评论都会有帮助。
非常感谢
发布于 2020-03-01 13:37:58
您可以使用requests下载文件,并使用apply将函数应用于数据文件的每一行:
import os
import requests
def download(row):
filename = os.path.join(root_folder,
'_'.join([row['Color'],
row['Gender'],
row['Model']],
str(row.name) + im_extension)
# create folder if it doesn't exist
os.makedirs(os.path.dirname(filename), exist_ok=True)
url = row.link
print(f"Downloading {url} to {filename}")
r = requests.get(url, allow_redirects=True)
with open(filename, 'wb') as f:
f.write(r.content)
root_folder = '/path/to/download/folder'
im_extension = '.jpg' # or whatever type of images you are downloading
df.apply(download, axis=1)https://stackoverflow.com/questions/60475568
复制相似问题