DEV Community

Eric The Coder
Eric The Coder

Posted on • Updated on

Python: Get and Save Google images with Selenium

Follow me!: Follow @EricTheCoder_



I create a small Python code to get and save images from Google images.
from selenium import webdriver
import time, requests

def search_google(search_query):
    browser = webdriver.Chrome()
    search_url = f"https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&q={search_query}"
    images_url = []

    # open browser and begin search
    browser.get(search_url)
    elements = browser.find_elements_by_class_name('rg_i')

    count = 0
    for e in elements:
        # get images source url
        e.click()
        time.sleep(1)
        element = browser.find_elements_by_class_name('v4dQwb')

        # Google image web site logic
        if count == 0:
            big_img = element[0].find_element_by_class_name('n3VNCb')
        else:
           big_img = element[1].find_element_by_class_name('n3VNCb')

        images_url.append(big_img.get_attribute("src"))

        # write image to file
        reponse = requests.get(images_url[count])
        if reponse.status_code == 200:
            with open(f"search{count+1}.jpg","wb") as file:
                file.write(reponse.content)

        count += 1

        # Stop get and save after 5
        if count == 5:
            break

    return images_url

items = search_google('dog')
Enter fullscreen mode Exit fullscreen mode

Alt Text

Top comments (2)

Collapse
 
mashirali profile image
mashirali

Here is my code as i am using C#,

var driver = new ChromeDriver();

driver.Navigate().GoToUrl("google.com/search?q=wallpapers+pic...);

// These are commented three ways to select the list of images

    //IList<IWebElement> Imghref = driver.FindElements(By.XPath("//img[@jsname]"));

    //IList<IWebElement> Imghref = driver.FindElements(By.ClassName("rg_i"));

    //IList<IWebElement> Imghref = driver.FindElements(By.TagName("img"));

    IList<IWebElement> Imghref = driver.FindElements(By.ClassName("rg_i"));

    //BcuVif - n3VNCb     --- ClassNames which i have observed

    foreach (IWebElement eachLink in Imghref)
    {
        eachLink.Click();

        IWebElement Images = driver.FindElement(By.TagName("img"));
        //Console.WriteLine(Images.GetAttribute("class"));
        String ImageUrl = Images.GetAttribute("src");
        string ImageName = Images.GetAttribute("alt");
        Console.WriteLine("Image URL : " + ImageUrl);
        WebClient downloader = new WebClient();
        downloader.DownloadFile(ImageUrl, "D:\\VisualStudio Workspace\\Download-Images\\images\\" + ImageName + ".jpg");

    }
Enter fullscreen mode Exit fullscreen mode

Its not working... Kindly share a quick fix to this

Collapse
 
rahul523680 profile image
rahul kumar • Edited

Hi,

This a very nice post that would help me in my research. Can you please help me in changing this code to download products image from google.
I have a list of products name, I need to download products images from google(or any search engine) and store it to a folder(any). I tried with the help of stack but it is giving me error. I tried:
from google_images_download import google_images_download

response = google_images_download.googleimagesdownload()
arguments = {"keywords":"Polar bears,baloons,Beaches","limit":20,"print_urls":True}

paths = response.download(arguments)
print(paths)

which is giving me the folders only and not image. All photos can be stored in one folder too.