Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- import base64
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.common.exceptions import NoSuchElementException
- driver = webdriver.Chrome('./chromedriver.exe')
- driver.get('https://www.google.com/search?q=구글&tbm=isch')
- all_img_div = driver.find_elements_by_xpath('/html/body/div[2]/c-wiz/div[3]/div[1]/div/div/div/div/div[1]/div[1]/div')
- count = 1
- for img_div in all_img_div:
- try:
- img1 = img_div.find_element_by_xpath('a[1]/div[1]/img')
- except NoSuchElementException:
- continue
- img1.click()
- try:
- img2 = WebDriverWait(driver, 10).until(
- EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div[1]/div[1]/div/div[2]/a/img')))
- except NoSuchElementException:
- continue
- imgdata = img2.get_attribute('src').split(',')
- if(len(imgdata) == 2):
- with open(str(count) + ".png", "wb") as fh:
- fh.write(base64.urlsafe_b64decode(imgdata[1]))
- else:
- print(imgdata[0]) #알아서 저장
- count+=1
Advertisement
Add Comment
Please, Sign In to add comment