Open In App

Download Google Image Using Python and Selenium

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to download google Image using Python and Selenium

Installation

On the terminal of your PC, type the following command. If it triggers any error regarding pip then you need to 1st install pip on windows manually by python get-pip.py command then you can run the following command.

pip install selenium

We also need to install a web driver that will help us to automatically run the web browser. You can install Firefox web driver, Internet Explorer web driver, or Chrome web driver. In this article, we will be using Chrome Web Driver.

The automation script interacts with the webpage by finding the element(s) we specified. There are various ways to find the elements in a webpage. The simplest way is to select the HTML tag of the desired element and copy its XPath. To do this, simply Right-Click on the webpage, click on “Inspect”, and copy the desired element’s XPath. You can also use the name or CSS of the element if you want to.

HTML of Google Images’ Result

Below is the implementation:

Python3




from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
 
# What you enter here will be searched for in
# Google Images
query = "dogs"
 
# Creating a webdriver instance
driver = webdriver.Chrome('Enter-Location-Of-Your-Webdriver')
 
# Maximize the screen
driver.maximize_window()
 
# Open Google Images in the browser
 
# Finding the search box
box = driver.find_element_by_xpath('//*[@id="sbtc"]/div/div[2]/input')
 
# Type the search query in the search box
box.send_keys(query)
 
# Pressing enter
box.send_keys(Keys.ENTER)
 
# Function for scrolling to the bottom of Google
# Images results
def scroll_to_bottom():
 
    last_height = driver.execute_script('\
    return document.body.scrollHeight')
 
    while True:
        driver.execute_script('\
        window.scrollTo(0,document.body.scrollHeight)')
 
        # waiting for the results to load
        # Increase the sleep time if your internet is slow
        time.sleep(3)
 
        new_height = driver.execute_script('\
        return document.body.scrollHeight')
 
        # click on "Show more results" (if exists)
        try:
            driver.find_element_by_css_selector(".YstHxe input").click()
 
            # waiting for the results to load
            # Increase the sleep time if your internet is slow
            time.sleep(3)
 
        except:
            pass
 
        # checking if we have reached the bottom of the page
        if new_height == last_height:
            break
 
        last_height = new_height
 
 
# Calling the function
 
# NOTE: If you only want to capture a few images,
# there is no need to use the scroll_to_bottom() function.
scroll_to_bottom()
 
 
# Loop to capture and save each image
for i in range(1, 50):
   
    # range(1, 50) will capture images 1 to 49 of the search results
    # You can change the range as per your need.
    try:
 
      # XPath of each image
        img = driver.find_element_by_xpath(
            '//*[@id="islrg"]/div[1]/div[' +
          str(i) + ']/a[1]/div[1]/img')
 
        # Enter the location of folder in which
        # the images will be saved
        img.screenshot('Download-Location' + 
                       query + ' (' + str(i) + ').png')
        # Each new screenshot will automatically
        # have its name updated
 
        # Just to avoid unwanted errors
        time.sleep(0.2)
 
    except:
         
        # if we can't find the XPath of an image,
        # we skip to the next image
        continue
 
# Finally, we close the driver
driver.close()


Result:

Captured Images

Well, this is the simplest way to create an automation script. This small program can be your fun little project. This could be the starting point of your journey with Selenium. You can use Selenium to do different things like scrape news from Google News. So keep your mind open about new ideas and you might end up creating a great project with Selenium and Python.



Previous Article
Next Article

Similar Reads

Download Instagram Posts Using Python Selenium module
In this article, we will learn how we can download Instagram posts of a profile using Python Selenium module. Requirements:Google Chrome or FirefoxChrome driver(For Google Chrome) or Gecko driver(For Mozilla Firefox)Selenium package: It is a powerful tool for controlling a web browser through the program. It is functional for all browsers, works on
7 min read
Download File in Selenium Using Python
Prerequisite: Selenium Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e. Python, Java, C#, etc. We will be working with Python. Selenium Tutorial covers all topics such as– WebDriv
2 min read
Download Anything to Google Drive using Google colab
When we download/upload something from a cloud server, it gives more transfer rate as compared to a normal server. We can use Google Drive for storage as well as fast speed download. The problem is how to upload something to G-Drive direct from Internet. So, Here we will see a solution to upload anything to Google drive directly from Internet. All
2 min read
Python | Get a google map image of specified location using Google Static Maps API
Google Static Maps API lets embed a Google Maps image on the web page without requiring JavaScript or any dynamic page loading. The Google Static Maps API service creates the map based on URL parameters sent through a standard HTTP request and returns the map as an image one can display on the web page. To use this service, one must need an API key
2 min read
Python | Automate Google Search using Selenium
Google search can be automated using Python script in just 2 minutes. This can be done using selenium (a browser automation tool). Selenium is a portable framework for testing web applications. It can automatically perform the same interactions that any you need to perform manually and this is a small example of it. Mastering Selenium will help you
3 min read
Search Google Using Python Selenium
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. This article illustrates about how to use Selenium Python
1 min read
Automating Google meet using selenium in Python
Prerequisites: Selenium Python basics, Browser Automation using Selenium Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Sel
3 min read
Text Searching in Google using Selenium in Python
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we are going to see how to automate our browser. We can jus
3 min read
Google Maps Selenium automation using Python
Prerequisites: Browser Automation using Selenium Selenium is a powerful tool for controlling a web browser through the program. It is functional for all browsers, works on all major OS, and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. It can be installed using the below command: pip install
4 min read
Upload and Download files from Google Drive storage using Python
In this article, we are going to see how can we download files from our Google Drive to our PC and upload files from our PC to Google Drive using its API in Python. It is a REST API that allows you to leverage Google Drive storage from within your app or program. So, let's go ahead and write a Python script to do that. Requirements: Python (2.6 or
6 min read