简体   繁体   中英

how to download images from url and create folders with python in pandas

I am pretty new in python and coding;

I have a dataframe looks like below;

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://....

I want to download those image links and save them in a directory which based on color-gender-model combination.

In the end I need Black_Man_Sneakers folder and all related images(for this example first and sixth links) should be in that folder.

How should I start? any comment would be helpful

Thanks a lot

You can use requests to download files and apply to apply a function to each row of the dataframe:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM