-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.py
68 lines (53 loc) · 2.42 KB
/
scrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import time
import logging
import urllib.request
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
IMAGES_FOLDER = "images"
class ImageScraper:
def __init__(self, search_input):
self.search_input = search_input
self.base_dir = os.path.join(os.getcwd(),IMAGES_FOLDER, search_input)
self.driver = webdriver.Chrome()
self.wait = WebDriverWait(self.driver, 50)
def _create_directory(self):
os.makedirs(self.base_dir, exist_ok=True)
logging.info(f"Created directory: {self.base_dir}")
def _save_image(self, image_url, file_name):
try:
image_path = os.path.join(self.base_dir, file_name)
response = urllib.request.urlopen(image_url)
with open(image_path, 'wb') as f:
f.write(response.read())
logging.info(f"Saved image: {file_name}")
except Exception as e:
logging.error(f"Error saving image: {str(e)}")
def scrape_images(self, num_images):
self._create_directory()
try:
self.driver.get(f'https://www.google.com/search?q={self.search_input}&tbm=isch')
self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'rg_i')))
images = self.driver.find_elements(By.CLASS_NAME, 'rg_i')
logging.info(f"Found {len(images)} images for {self.search_input}")
while len(images) < num_images:
# Scroll to load more images
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
images = self.driver.find_elements(By.CLASS_NAME, 'rg_i')
logging.info(f"Loaded {len(images)} images")
for index in range(num_images):
image = images[index]
image_url = image.get_attribute('src')
file_name = f"{self.search_input}_{index}.png"
self._save_image(image_url, file_name)
except Exception as e:
logging.error(f"Error scraping images: {str(e)}")
finally:
self.driver.quit()
search_input = "moon" #Enter search term
num_images = 50 #Enter the number of images to download
logging.basicConfig(level=logging.INFO)
ImageScraper(search_input).scrape_images(num_images)