Guest User

Untitled

a guest
May 18th, 2020
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. from selenium import webdriver
  2. import base64
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.common.exceptions import NoSuchElementException
  7. driver = webdriver.Chrome('./chromedriver.exe')
  8. driver.get('https://www.google.com/search?q=구글&tbm=isch')
  9.  
  10.  
  11. 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')
  12. count = 1
  13. for img_div in all_img_div:
  14. try:
  15. img1 = img_div.find_element_by_xpath('a[1]/div[1]/img')
  16. except NoSuchElementException:
  17. continue
  18. img1.click()
  19. try:
  20. img2 = WebDriverWait(driver, 10).until(
  21. 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')))
  22. except NoSuchElementException:
  23. continue
  24. imgdata = img2.get_attribute('src').split(',')
  25. if(len(imgdata) == 2):
  26. with open(str(count) + ".png", "wb") as fh:
  27. fh.write(base64.urlsafe_b64decode(imgdata[1]))
  28. else:
  29. print(imgdata[0]) #알아서 저장
  30. count+=1
Advertisement
Add Comment
Please, Sign In to add comment