Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

how to scrape image from google in selenium python

Write a python program to access the search bar and search button on images.google.com and scrape 10
images each for keywords ‘fruits’, ‘cars’ and ‘Machine Learning’, ‘Guitar’, ‘Cakes’

how to apply for loop to static list of array
Difference between enumerate and range methods
After submit method apply time.sleep method
how to apply print for debug
how to get only 10 element using enumerate data type
difference between range and enumerate data type
How to create directory using os.makedirs
how to retrieve content from a URL (in this case, an image) and save it to a local file using urllib.request.urlretrieve.
how to scroll 20 times untill 100 image get using driver.execute script
how to use breakby.xpath
how to apply http validation first four char of string to store image in list
How to save image from image url in python
take len(string) parameter inside range

how to save file using file.open
file handling operation in python

Step 1: install selenium using python

pip install --upgrade selenium
Enter fullscreen mode Exit fullscreen mode

Step 2: Import Required Libraries

import pandas as pd
from selenium import webdriver
import warnings
warnings.filterwarnings('ignore')
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException,ElementClickInterceptedException

Enter fullscreen mode Exit fullscreen mode

**Step 3: Define a list of keywords that you want to search for on Google Images.

keywords = ['fruits', 'cars', 'Machine Learning', 'Guitar', 'Cakes']
Enter fullscreen mode Exit fullscreen mode

step4:scrape image using Loop through each keyword:

for keyword in keywords:
    # Create a directory for the current keyword
    save_dir = f'downloaded_images/{keyword}'
    os.makedirs(save_dir, exist_ok=True)

    # Navigate to Google Images
    driver.get('https://www.google.com/imghp')

    # Find the search bar and enter the keyword
    search_bar = driver.find_element(By.NAME, 'q')
    search_bar.send_keys(keyword)
    search_bar.submit()

    time.sleep(2)  # Wait for the page to load

    # Collect image elements
    image_elements = driver.find_elements(By.CSS_SELECTOR, 'img.rg_i')

    # Download and save the first 10 images for the current keyword
    for i, img_element in enumerate(image_elements[:10]):
        img_url = img_element.get_attribute('src')
        try:
            urllib.request.urlretrieve(img_url, os.path.join(save_dir, f'{keyword}_{i + 1}.jpg'))
            print(f'Saved image for {keyword}: {i + 1}')
        except Exception as e:
            print(f'Error saving image for {keyword}: {str(e)}')
Enter fullscreen mode Exit fullscreen mode

output

Image description

Image description

Image description

=======================================

enumerate

:

enumerate is a built-in function in Python used for iterating over an iterable (e.g., a list) while keeping track of both the index (position) and the value from the iterable.
It returns an iterable of tuples, where each tuple contains two elements: the index and the corresponding value from the original iterable.
It's commonly used in for loops when you need to access both the index and the value of elements in a sequence.
Example:

fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
    print(f"Index {index}: {fruit}")
Enter fullscreen mode Exit fullscreen mode

Output:

Index 0: apple
Index 1: banana
Index 2: cherry
Enter fullscreen mode Exit fullscreen mode

range

:

range is a built-in function in Python used for generating a sequence of numbers within a specified range.
It returns an iterable sequence of numbers, typically used in for loops to iterate a specific number of times or to generate a series of indices.
It is often used to control the number of iterations in a loop or to create lists of numbers.
Example:

for i in range(3):
    print(i)
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
Enter fullscreen mode Exit fullscreen mode

Note that range itself does not directly provide values from a list; it generates a sequence of numbers that you can use as indices or to control iterations.

==========================================
how to scroll 20 times untill 100 image get using driver.execute script
how to save file using file.open
how to use breakby.xpath
how to apply http validation to store image in list

Image description

How to save image from image url in python

import requests

URL of the image you want to download

image_url = "https://example.com/image.jpg"

try:
    # Send an HTTP GET request to the image URL
    response = requests.get(image_url)

    # Check if the request was successful (status code 200)
    if response.status_code == 200:
        # Open a file in binary write mode to save the image
        with open("downloaded_image.jpg", "wb") as file:
            # Write the binary content of the image to the file
            file.write(response.content)
        print("Image saved successfully as 'downloaded_image.jpg'")
    else:
        print(f"Failed to download image. Status code: {response.status_code}")
except Exception as e:
    print(f"An error occurred: {str(e)}")
Enter fullscreen mode Exit fullscreen mode

Image description

file handling operation in python

File handling in Python allows you to perform various operations on files, such as reading from them, writing to them, creating new files, and more. Here's an explanation of common file handling operations in Python with examples:

Opening a File:

To work with a file, you first need to open it. You can use the open() function, which takes two arguments: the file path and the mode (read, write, append, etc.). The mode can be specified as a string, such as 'r' for reading, 'w' for writing, 'a' for appending, and so on.

# Opening a file for reading
file = open("example.txt", "r")

# Opening a file for writing (creates a new file if it doesn't exist)
file = open("output.txt", "w")
Enter fullscreen mode Exit fullscreen mode

Reading from a File:

To read the content of a file, you can use various methods, such as read(), readline(), or readlines().

# Reading the entire content of the file
content = file.read()

# Reading one line at a time
line = file.readline()
Enter fullscreen mode Exit fullscreen mode

Writing to a File:

To write data to a file, you can use the write() method.

# Writing a string to the file
file.write("Hello, World!\n")

You should remember to close the file after writing using the close() method.


file.close()
Enter fullscreen mode Exit fullscreen mode

Appending to a File:

To append data to an existing file, you can open the file in append mode ('a') and then use the write() method.

# Appending data to an existing file
file = open("existing.txt", "a")
file.write("New data to append\n")
file.close()
Enter fullscreen mode Exit fullscreen mode

Image description

Working with Binary Files:

Binary files, such as images or non-text files, can be opened and manipulated using the 'rb' (read binary) and 'wb' (write binary) modes.

# Opening a binary file for reading
binary_file = open("image.jpg", "rb")

# Opening a binary file for writing
binary_file = open("output.bin", "wb")
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Using Context Managers (Recommended):

It's recommended to use a context manager (with statement) when working with files. The context manager ensures that the file is automatically closed when you're done with it, even if an error occurs.

with open("example.txt", "r") as file:
    content = file.read()
Enter fullscreen mode Exit fullscreen mode

File is automatically closed when the block is exited

Iterating Through Lines of a File:

You can use a for loop to iterate through the lines of a file.

with open("example.txt", "r") as file:
    for line in file:
        print(line)
Enter fullscreen mode Exit fullscreen mode

These are the basic file handling operations in Python. Remember to handle exceptions, like FileNotFoundError or PermissionError, when working with files to make your code more robust.

Top comments (0)