![](/img/trans.png)
[英]Download web images by URL from excel and save to folders in Python
[英]how to download images from url and create folders with python in pandas
我在 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 文件夹,并且所有相关图像(对于本示例中的第一个和第六个链接)都应该在该文件夹中。
我应该如何开始? 任何评论都会有帮助
非常感谢
您可以使用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.