How to, Programming

Python | How to download all the images from the website?

python

python programming

import requests

 The requests module allows you to send HTTP requests using Python. The HTTP request returns a Response Object with all the response data (content, encoding, status, etc. Here in our case we will try to fetch images). First of all, you will need to download and install request module, for that type in the following on your command:

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip install requests

Now lets import BeautifulSoup for web scraping which is needed to scarp images from a particular website. To use beautiful soup, you need to install it:

$ pip install beautifulsoup4
from bs4 import BeautifulSoup as bs 

The last thing we need to import for our project is the OS module. The main purpose of the OS module is to interact with your operating system. The primary use I find for it is to create folders, remove folders, move folders, and sometimes change the working directory. In our project we will be using the OS module to create a folder to save all the images we downloaded from the website.

The OS module is a part of the standard library, or stdlib, within Python 3. This means that it comes with your Python installation, but you still must import it.

import os

Declare the website you want to download images from :

url = 'https://stock.adobe.com/search?load_type=search&is_recent_search=&k=motivational&native_visual_search=&similar_content_id='

Now we will need to download the page for parsing :

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

We have to locate all the elements with image tag in that particular website :

image_tags = soup.findAll('img')

Lets create a directory to save all the images we download from the website:

if not os.path.exists('quotes'):  //you can give it any name instead of quotes
    os.makedirs('quotes')

Lets move to the directory we just created :

os.chdir('quotes')

We need to give a name to the images we will be downloading :

x = 0

To write the images:

for image in image_tags:     //just a for loop for image_tags
    try:
        url = image['src']
        response = requests.get(url)
        if response.status_code == 200:  // It's a HTTP status code, it means "OK" (EG: The server successfully answered the http request).

with open('quote-' + str(x) + '.jpg', 'wb') as f: //just assigning the name to the images
                f.write(requests.get(url).content)  //writing file
                f.close()            //closing file
                x += 1               //images name will be in ascending number

except:
pass

 That’s all. Now you will be able to download all the images from the website using python.

Here’s the 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

Follow the writer on github : https://github.com/SantoshAcharya1200

Spread the love

Leave a Reply