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.