0

I am trying to download all images that appear in Google image search

I am using "Selenium" with Python, Pycharm

When I check Chrome developer tool to get Xpath i got the following:

//[@id="islrg"]/div[1]/div[1]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[24]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[26]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[49]/a[1]/div[1]/img

//[@id="islrg"]/div[1]/div[51]/div[1]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[51]/div[24]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[51]/div[26]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[51]/div[49]/a[1]/div[1]/img //*[@id="islrg"]/div[1]/div[51]/div[51]/a[1]/div[1]/img

//[@id="islrg"]/div[1]/div[52]/div[20]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[52]/div[45]/a[1]/div[1]/img //*[@id="islrg"]/div[1]/div[52]/div[47]/a[1]/div[1]/img

//[@id="islrg"]/div[1]/div[52]/div[70]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[52]/div[72]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[52]/div[95]/a[1]/div[1]/img //[@id="islrg"]/div[1]/div[53]/div[18]/a[1]/div[1]/img

1- How to get dynamically Xpath?!

and then trying to get the actual image url but i got encrypted ones (https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTWbVxeA8AzNYZoHKN91jM2UMG_g1pjlWN5kQ&usqp=CAU) by example?

I post my whole code here:

import os
import time
import requests  # Import the requests module
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from PIL import Image
import io
import pandas as pd
import sys

import msvcrt, time

books = []
def get_images_from_google(driver, delay, max_images):
    def scroll_down(driver):
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(delay)

    image_urls = set()
    skips = 0

    while len(image_urls) + skips < max_images:
        scroll_down(driver)

        thumbnails = driver.find_elements(By.CSS_SELECTOR, '.rg_i, .Q4LuWd')
        for img in thumbnails[len(image_urls) + skips:max_images]:
            try:
                img.click()
                time.sleep(delay)
            except:
                continue

            images = driver.find_elements(By.CSS_SELECTOR, '.sFlh5c, .pT0Scc, .iPVvYb')
            for image in images:
                if image.get_attribute('src') in image_urls:
                    max_images += 1
                    skips += 1
                    break

                if image.get_attribute('src') and 'http' in image.get_attribute('src'):
                    image_urls.add(image.get_attribute('src'))
                    print(f"Found {len(image_urls)}")


    return image_urls

def download_image(download_path, url, file_name):
    try:
        image_content = requests.get(url).content
        image_file = io.BytesIO(image_content)
        image = Image.open(image_file)
        print(url)

        # Check if the image can be identified and is in a compatible format
        if image.format not in ["JPEG", "PNG"]:
            print(f"Skipping image with unsupported format: {url}")
            return

        file_path = os.path.join(download_path, file_name)  # Use os.path.join to ensure correct path

        with open(file_path, "wb") as f:
            image.save(f, "JPEG")

        print("Success")
    except Exception as e:
        print('FAILED -', e)


# Ask the user for the search query
search_query = input("Enter your Google Images search query: ")

# Create the 'imgs/' directory if it doesn't exist
download_path = "imgs"
os.makedirs(download_path, exist_ok=True)

# Create a Chrome driver
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=options)

# Open the Google Images search page with the provided search query
search_url = f"https://www.google.com/search?q={search_query}&tbm=isch"
driver.get(search_url)

# Perform image scraping and downloading
urls = get_images_from_google(driver, 0.01, 10000)


for i, url in enumerate(urls):
        download_image(download_path, url, str(i) + ".JPEG")
        books.append([url])


df = pd.DataFrame(books, columns=['URL'])
df.to_csv('books.csv')
# Close the driver instance
driver.quit()

2-how to improve it to get the actual URL link of each image?

1 Answer 1

0

1- You can use xpath to get everything related to that 'img' tag. In my test, I observed that image results are children under with @role='button', so I used this xpath: //a[@role='button']//img

2- I dont think it has any gif in the results, all image's thumbnails are either jpg or png, and it is safe to same them both as jpg.

ps: I usually use this cheatsheet when working with xpath/css https://devhints.io/xpath

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.