-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_images.py
More file actions
46 lines (37 loc) · 1.31 KB
/
extract_images.py
File metadata and controls
46 lines (37 loc) · 1.31 KB
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
import os
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
# Set up the webdriver
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 10)
df = pd.read_csv('all_products_with_deatails.csv')
urls = df['Links']
if not os.path.exists('images/'):
os.mkdir('images')
titles = 1687
for url in urls:
driver.get(url)
title = driver.title
print(title)
driver.implicitly_wait(5)
wait = WebDriverWait(driver, 5)
# Find all image elements
image_elements = driver.find_elements(By.TAG_NAME, 'img')
print(f"Found {len(image_elements)} images on the page.")
# Create a directory to save the images
if not os.path.exists(f'images/{titles}'):
os.mkdir(f'images/{titles}')
# Loop through the image elements and save each image
for i, img in enumerate(image_elements):
src = img.get_attribute('src')
if src is not None and 'http' in src:
response = requests.get(src)
with open(f'images/{titles}/image_{i}.jpg', 'wb') as f:
f.write(response.content)
print(f"Saved image_{i}.jpg")
titles += 1