简体   繁体   English

Python 将图像从 URL 保存到具有自定义名称和结构的不同文件夹中

[英]Python save images from URL to different folders with custom name and structure

I have a.csv file with image urls, and I need to save each image in a different folder like the tree in URL.我有一个带有图像网址的.csv 文件,我需要将每个图像保存在不同的文件夹中,例如 URL 中的树。 For example, here 3 URL比如这里3 URL

https://citynews.stgy.ovh/~shared/uploads/files/794/794964898449/62325865102181/logo_1.pic
https://citynews.stgy.ovh/~shared/uploads/files/420/42030573107817/62325865102181/logo_1.pic
https://citynews.stgy.ovh/~shared/uploads/files/218/21897603939378/62325865102181/logo_1.pic

I need that every image is saved in a different folder tree named as path url, like this:我需要将每个图像保存在名为路径 url 的不同文件夹树中,如下所示:

  • image 1 -> folder /uploads/files/794/794964898449/62325865102181/图片 1 -> 文件夹 /uploads/files/794/794964898449/62325865102181/
  • image 2 -> folder /uploads/files/420/42030573107817/62325865102181/图片 2 -> 文件夹 /uploads/files/420/42030573107817/62325865102181/
  • image 3 -> folder /uploads/files/218/21897603939378/62325865102181/图 3 -> 文件夹 /uploads/files/218/21897603939378/62325865102181/

This should do it:这应该这样做:

import os
import urllib.request as urllib2

BASE_DIR = "C:\\workspace"

urls = ["https://citynews.stgy.ovh/~shared/uploads/files/794/794964898449/62325865102181/logo_1.pic",
        "https://citynews.stgy.ovh/~shared/uploads/files/420/42030573107817/62325865102181/logo_1.pic",
        "https://citynews.stgy.ovh/~shared/uploads/files/218/21897603939378/62325865102181/logo_1.pic"
        ]

for url in urls:
    split_path = url.split("~shared/")[1]
    img_name = os.path.basename(split_path)
    dir_name = os.path.dirname(split_path)
    path = os.path.join(BASE_DIR, dir_name)
    print(path)
    os.makedirs(path, exist_ok = True)
    urllib2.urlretrieve(url, os.path.join(path, img_name))

print("done")

Also, read this link: python-os-makedirs另外,请阅读此链接: python-os-makedirs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过URL从Excel下载Web图像并保存到Python文件夹中 - Download web images by URL from excel and save to folders in Python 在python中将切片图像保存到不同的文件夹中 - Save the sliced images into a different folders in python 自定义文件结构可在python中保存多个图像 - Custom file structure to save multiple images in python 如何将处理后的图像保存到python文件夹中的其他文件夹中? - How to save processed images to different folders within a folder in python? 如何使用循环将图像保存在python-opencv的不同文件夹中 - How to use a loop to save images in different folders in python-opencv 如何从url下载图像并在pandas中使用python创建文件夹 - how to download images from url and create folders with python in pandas 如何从python中的不同文件夹和子文件夹加载数据(图像) - How to load data(images) from different folders and subfolders in python 如何从python中的不同文件夹和子文件夹加载图像 - how to load images from different folders and subfolders in python 如何通过Python合并来自同名但位于不同文件夹中的文件的内容? - How to merge content from files with same name but in different folders by Python? python 以编程方式从不同文件夹导入相同的文件名 - python import the same file name programatically from different folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM