-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.py
More file actions
50 lines (45 loc) · 1.7 KB
/
scrape.py
File metadata and controls
50 lines (45 loc) · 1.7 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
47
48
49
50
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from urllib.request import urlretrieve
import os
import time
import ssl
def initialize():
try:
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.maximize_window()
count=0
for i in range(1, searchPage + 1):
url = your_url + "&page=" + str(i)
driver.get(url)
data = driver.execute_script("return document.documentElement.outerHTML")
print("Page " + str(i))
scraper = BeautifulSoup(data, "lxml")
img_container = scraper.find_all("img", {"class":"z_g_a z_g_b"})
for j in range(0, len(img_container)-1):
img_src = img_container[j].get("src")
name = img_src.rsplit("/", 1)[-1]
try:
urlretrieve(img_src, os.path.join(scrape_directory, os.path.basename(img_src)))
print("Scraped " + name)
count+=1
except Exception as e:
print(e)
driver.close()
except Exception as e:
print(e)
os.makedirs("images")
scrape_directory = "./images"
## Enter your url in this string
your_url = "https://www.shutterstock.com/search/indian+face?number_of_people=1&mreleased=true&image_type=photo"
#Number of pages
searchPage = 30
initialize()
print("Scraping complete.")
print("Images scraped=", count)