Skip to content
Advertisement

Save Images with orginal name

I have script to download images from website. But it’s saves name with ‘images1, images2, images3, images4 etc’

I need to save images with orginal name. If the images name 43343.jpg i need to save with 43343.jpg I use beautifulsoup and requests for this case.

Sorry my english. It’s not my first language

from bs4 import *
import requests
import os

def folder_create(images):
    try:
        folder_name = input("Enter Folder Name:- ")
        os.mkdir(folder_name)
    
    except:
        print("Folder Exist with that name!")
        folder_create()

    
    download_images(images, folder_name)



def download_images(images, folder_name):
    count = 0

    
    print(f"Total {len(images)} Image Found!")

    
    if len(images) != 0:
        for i, image in enumerate(images):
            

                        
                        
                        
                        

            

            
            try:
                
                image_link = image["data-srcset"]
                
            
            
            except:
                try:
                    
                    image_link = image["data-src"]
                except:
                    try:
                        
                        image_link = image["data-fallback-src"]
                    except:
                        try:
                            
                            image_link = image["src"]

                        
                        except:
                            pass

            
            
            try:
                r = requests.get(image_link).content
                try:

                    
                    r = str(r, 'utf-8')

                except UnicodeDecodeError:

                    
                    with open(f"{folder_name}/images{i+1}.jpg", "wb+") as f:
                        f.write(r)

                    
                    count += 1
            except:
                pass

         
         
         
        if count == len(images):
            print("All Images Downloaded!")
            
        
        else:
            print(f"Total {count} Images Downloaded Out of {len(images)}")


def main(url):

    
    r = requests.get(url)

    
    soup = BeautifulSoup(r.text, 'html.parser')

    
    images = soup.findAll('img')

    
    folder_create(images)



url = input("Enter URL:- ")


main(url) ```

Advertisement

Answer

Try replacing this part of your code

with open(f"{folder_name}/images{i+1}.jpg", "wb+") as f:
    f.write(r)

with this:

image_name = image_link.split('/')[-1]
with open(f"{folder_name}/{image_name}.jpg", "wb+") as f:
    f.write(r)

This will get the last element of your image_path (after the last slash), which should be the name of the image, given the rest of your code is correct and I understood the works of it correctly.

Also, I don’t know what your image_link variable looks like but maybe you need to replace the / with \ if your path uses backslashes.


On a last note, consider putting a bit more effort into your stackoverflow questions or your code quality in general. The sheer amount of empty lines makes the code really tedious to read and all the nesting (going deeper and deeper with try-except clauses) certainly doesn’t help with that either.

Aside from that, there are lots of better ways to achieve your intended functionality without multiple bare except clauses. In fact, you should never use a bare except statement (`except:` without an exception classname after it). But that’s a story for another time.
Advertisement