Programming

Making a python program which downloads all the images from websites

python,coding
Spread the love

This is a simple python script that I wrote to download images from different urls. First of all lets import some modules.

import requests
from bs4 import BeautifulSoup as bs
import os

Requests is a Python module that you can use to send all kinds of HTTP requests.  Requests is an external module, so you will have to install it first. You can install it by running the following command in the terminal:

pip install requests

Beautiful Soup is a Python library for pulling data out of HTML and XML files. Here we will be using it to parse images from the website. It is an external module, so you have to install it manually. To do so:

pip install beautifulsoup4

The OS module in Python provides a way of using operating system dependent functionality. The functions that the OS module provides allows you to interface with the underlying operating system that Python is running on – be that Windows, Mac or Linux.

Now lets declare the website with the images in url.

url = ‘https://your_website_name.com ‘

Now download the page for parsing.

page = requests.get(url)
soup = bs(page.text, ‘html.parser’)

Now we have to find all the images located in that website.

image_tags = soup.findAll(‘img’)

After that we will use os module to create directory to store all the downloaded images.

if not os.path.exists(‘folder_name’):
    os.makedirs(‘folder_name’)

Just to make sure we are in the right directory, use the following code.

os.chdir(‘folder_name’)

x = 0   //using this variable for the name of images downloaded.

Now we will iterate through all the images to give a image name. Make sure to open images as ‘wb’ where b stands for binary which is necessary to work with images.

for image in image_tags:
    try:
        url = image[‘src’]
        response = requests.get(url)
        if response.status_code == 200:
            with open(‘quote-‘ + str(x) + ‘.jpg’, ‘wb’) as f:
                f.write(requests.get(url).content)
                f.close()
                x += 1
    except:
        pass

The images will be downloaded in ‘folder_name’ directory

Java vs kotlin vs flutter for android development 2019?

Full Code

import requests
from bs4 import BeautifulSoup as bs
import os

# website with model images
url = ‘https://stock.adobe.com/search?load_type=search&is_recent_search=&k=motivational&native_visual_search=&similar_content_id=’

# download page for parsing
page = requests.get(url)
soup = bs(page.text, ‘html.parser’)

# locate all elements with image tag
image_tags = soup.findAll(‘img’)

# create directory for model images
if not os.path.exists(‘quotes’):
    os.makedirs(‘quotes’)

# move to new directory
os.chdir(‘quotes’)

# image file name variable
x = 0

# writing images
for image in image_tags:
    try:
        url = image[‘src’]
        response = requests.get(url)
        if response.status_code == 200:
            with open(‘quote-‘ + str(x) + ‘.jpg’, ‘wb’) as f:
                f.write(requests.get(url).content)
                f.close()
                x += 1
    except:
        pass

Leave a Reply

Your email address will not be published. Required fields are marked *