Download images from Google using python
Tags: Python, Automation, Selenium Webdriver, Selenium

We all know that python is a very good and easy language for programming. It makes our lives easy :) that’s the reason why people like it. We can save our valuable time by automating things in python.

You may need image dataset for implementing image classification or anything (when learning with an idea you got). You need not waste your time by manually downloading images from Google. Here let me write something for you that really helps you.

We can do this using web scraping. selenium, a package in python helps us to do web scraping. For further info- https://www.seleniumhq.org/

install selenium

pip install selenium

type this command in terminal and hit enter

Then you want to use Selenium WebDriver; a collection of language-specific bindings to drive a browser — the way it is meant to be driven.

So you need to download a WebDriver according to the browser you use. Download a WebDriver Here. Remember where you are downloading the WebDriver, You will need the path for execution.

Okay, now you have been set up the environment for selenium and WebDriver. So, Let’s dive into the code.

from selenium import webdriver

import time

import urllib.request

import os

from selenium.webdriver.common.keys import Keys

importing all required things for later use

browser = webdriver.Chrome(“path\\to\\the\\webdriverFile”) #incase you are chrome browser.get(“ https://www.google.com/ ")

we can any use URL as a parameter for webdriver.get(). It opens the URL in the browser.

search = browser.find_element_by_name(‘q’)

To get the search bar field from the Google home page.

search.send_keys(key_words,Keys.ENTER)

In the place of key_words, you need to pass keywords you want to get images in the form of String. It enters the keywords passed into the search field and hits ENTER

elem = browser.find_element_by_link_text(‘Images’) elem.get_attribute(‘href’) elem.click()

Then we need to get into the images page to download images. get_attribute fetches the URL of the Images. And then clicks on the URL.

value = 0

for i in range(20):

browser.execute_script(“scrollBy(“+ str(value) +”,+1000);”)

value += 1000

time.sleep(3)

The above code is just to scroll down the page for loading all images

elem1 = browser.find_element_by_id(‘islmp’)

It is to get element by id with value ‘islmp’. Google images contain in a div tag with is ‘islmp’. That’s the reason to fetch it.

sub = elem1.find_elements_by_tag_name(“img”)

images contain in the div tag that has been fetched earlier. By using that element we will all img elements in the div tag.

try:

os.mkdir(‘downloads’)

except FileExistsError:

pass

creating a folder in the present directory for saving images

count = 0

for i in sub:

src = i.get_attribute('src')

try:

if src != None:

src = str(src)

print(src)

count+=1

urllib.request.urlretrieve(src, os.path.join('downloads','image'+str(count)+'.jpg'))

else:

raise TypeError

except TypeError:

print('fail')

looping all the img elements we get earlier. we may get some None values for the image URL. To make not to get errors we must use try and except. urllib.request.urlretrieve is used for downloading files.

Thanks for scrolling down :)

please clap, if it’s helpful 🙂

I will be very happy if you let me know my mistakes if exists ;)