How to download google image search results in Python

Viewed 48672

This question has been asked numerous times before, but all answers are at least a couple years old and currently based on the ajax.googleapis.com API, which is no longer supported.

Does anyone know of another way? I'm trying to download a hundred or so search results, and in addition to Python APIs I've tried numerous desktop, browser-based, or browser-addon programs for doing this which all failed.

10 Answers

Make sure you install icrawler library first, use.

pip install icrawler
from icrawler.builtin import GoogleImageCrawler
google_Crawler = GoogleImageCrawler(storage = {'root_dir': r'write the name of the directory you want to save to here'})
google_Crawler.crawl(keyword = 'sad human faces', max_num = 800)

Improving a bit on Ravi Hirani's answer the simplest way is to go by this :

from icrawler.builtin import GoogleImageCrawler

google_crawler = GoogleImageCrawler(storage={'root_dir': 'D:\\projects\\data core\\helmet detection\\images'})
google_crawler.crawl(keyword='cat', max_num=100)

Source : https://pypi.org/project/icrawler/

I'm trying this library that can be used as both: a command line tool or a python library. It has lots of arguments to find images with different criterias.

Those are examples taken from its documentation, to use it as a python library:

from google_images_download import google_images_download   #importing the library

response = google_images_download.googleimagesdownload()   #class instantiation

arguments = {"keywords":"Polar bears,baloons,Beaches","limit":20,"print_urls":True}   #creating list of arguments
paths = response.download(arguments)   #passing the arguments to the function
print(paths)   #printing absolute paths of the downloaded images

or as a commandline tool, as follows:

$ googleimagesdownload --k "car" -sk 'red,blue,white' -l 10

You can install this with pip install google_images_download

A simple solution to this problem is to install a python package called google_images_download

pip install google_images_download

use this python code

from google_images_download import google_images_download  

response = google_images_download.googleimagesdownload()
keywords = "apple fruit"
arguments = {"keywords":keywords,"limit":20,"print_urls":True}
paths = response.download(arguments)
print(paths)

adjust the limit to control the no of images to download

but some images won't open as they might be corrupt

change the keywords String to get the output you need

I have tried many codes but none of them working for me. I am posting my working code here. Hope it will help others.

I am using Python version 3.6 and used icrawler

First, you need to download icrawler in your system.

Then run below code.

from icrawler.examples import GoogleImageCrawler
google_crawler = GoogleImageCrawler()
google_crawler.crawl(keyword='krishna', max_num=100)

Replace keyword krishna with your desired text.

Note:- Downloaded image needs path. Right now I used same directory where script placed. You can set custom directory via below code.

google_crawler = GoogleImageCrawler('path_to_your_folder')
Related