首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从url下载图片并在熊猫中创建包含python的文件夹

如何从url下载图片并在熊猫中创建包含python的文件夹
EN

Stack Overflow用户
提问于 2020-03-01 13:26:27
回答 1查看 1.2K关注 0票数 0

我在python和编码方面很新;

我有一个数据,如下所示;

代码语言:javascript
运行
复制
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文件夹,所有相关的图像(在本例中,第一和第六链接)都应该在该文件夹中。

我该怎么开始?任何评论都会有帮助。

非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-01 13:37:58

您可以使用requests下载文件,并使用apply将函数应用于数据文件的每一行:

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60475568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档