Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Chromedriver Automated image downloader not working.

SaffronEmu

New Coder
Python:
import os
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
 
# Prompt user for search term and number of images to download
search_term = input("Enter search term: ")
num_images = int(input("Enter number of images to download: "))
 
# Set up the webdriver
driver_path = r"IMAGINEFILELOCATIONHERE"
service = Service(driver_path)
driver = webdriver.Chrome(service=service)
 
# Navigate to Google Images
driver.get("https://images.google.com/")
 
# Find the search bar and enter the search term
search_bar = driver.find_element(By.NAME, "q")
search_bar.send_keys(search_term)
search_bar.send_keys(Keys.RETURN)
 
# Scroll down to load more images
last_height = driver.execute_script("return document.body.scrollHeight")
while len(driver.find_elements(By.CSS_SELECTOR, "img.rg_i")) < num_images:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(3)
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
 
# Find the image thumbnails and download the images
image_thumbnails = driver.find_elements(By.CSS_SELECTOR, "img.rg_i")
directory = r"IMAGINETHEFILEHERE".format(search_term)
os.makedirs(search_term, exist_ok=True)
for i in range(num_images):
    try:
        image_thumbnails[i].click()
        time.sleep(10)
        image_urls = driver.find_elements(By.CSS_SELECTOR, "img.n3VNCb")
        if len(image_urls) == 0:
            print("Error: Could not find full-sized image. Please check page source for more information.")
            with open(f"{search_term}_page_source.html", "w", encoding="utf-8") as f:
                f.write(driver.page_source)
            break
        for j, image_url in enumerate(image_urls):
            print(f"Downloading image {i+1}-{j+1}/{num_images} from URL: {image_url.get_attribute('data-src')}")
            image_data = requests.get(image_url.get_attribute('data-src')).content
            with open(os.path.join(search_term, f"{search_term}_{i}-{j}.jpg"), "wb") as f:
                f.write(image_data)
            print(f"Downloaded image {i+1}-{j+1}/{num_images}")
    except Exception as e:
        print(f"Error downloading image {i+1}/{num_images}: {e}")
    if i == len(image_thumbnails) - 1:
        break
 
# Close the webdriver
driver.quit()


Hi, My code doesnt work, and I think its to do with Chrome's updated HTML protection services, let me know if you find anything to help.
 
Hey.

The issue i am getting says :-
Code:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Is this the error you get?
 
Python:
import os
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
 
# Prompt user for search term and number of images to download
search_term = input("Enter search term: ")
num_images = int(input("Enter number of images to download: "))
 
# Set up the webdriver
driver_path = r"IMAGINEFILELOCATIONHERE"
service = Service(driver_path)
driver = webdriver.Chrome(service=service)
 
# Navigate to Google Images
driver.get("https://images.google.com/")
 
# Find the search bar and enter the search term
search_bar = driver.find_element(By.NAME, "q")
search_bar.send_keys(search_term)
search_bar.send_keys(Keys.RETURN)
 
# Scroll down to load more images
last_height = driver.execute_script("return document.body.scrollHeight")
while len(driver.find_elements(By.CSS_SELECTOR, "img.rg_i")) < num_images:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(3)
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
 
# Find the image thumbnails and download the images
image_thumbnails = driver.find_elements(By.CSS_SELECTOR, "img.rg_i")
directory = r"IMAGINETHEFILEHERE".format(search_term)
os.makedirs(search_term, exist_ok=True)
for i in range(num_images):
    try:
        image_thumbnails[i].click()
        time.sleep(10)
        image_urls = driver.find_elements(By.CSS_SELECTOR, "img.n3VNCb")
        if len(image_urls) == 0:
            print("Error: Could not find full-sized image. Please check page source for more information.")
            with open(f"{search_term}_page_source.html", "w", encoding="utf-8") as f:
                f.write(driver.page_source)
            break
        for j, image_url in enumerate(image_urls):
            print(f"Downloading image {i+1}-{j+1}/{num_images} from URL: {image_url.get_attribute('data-src')}")
            image_data = requests.get(image_url.get_attribute('data-src')).content
            with open(os.path.join(search_term, f"{search_term}_{i}-{j}.jpg"), "wb") as f:
                f.write(image_data)
            print(f"Downloaded image {i+1}-{j+1}/{num_images}")
    except Exception as e:
        print(f"Error downloading image {i+1}/{num_images}: {e}")
    if i == len(image_thumbnails) - 1:
        break
 
# Close the webdriver
driver.quit()


Hi, My code doesnt work, and I think its to do with Chrome's updated HTML protection services, let me know if you find anything to help.
HI there,
Can you please give a brief overview of your script here? What is the intended purpose?
 

New Threads

Buy us a coffee!

Back
Top Bottom