### 필요 라이브러리설치 및 크롬 드라이버 설치
## 셀레니움을 사용하기 위해서는 크롬드라이버가 필요합니다
!pip install selenium
!apt-get update
!apt install chromium-chromedriver
# 필요 라이브러리 추가
from selenium import webdriver
import urllib
from bs4 import BeautifulSoup as bs
from selenium.webdriver.common.keys import Keys
import time
import os
import subprocess
def imageDown(keyword):
url = f'<https://www.google.com.br/search?q={keyword}&source=lnms&tbm=isch>'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver', chrome_options=chrome_options)
driver.get(url)
body = driver.find_element_by_css_selector('body')
# 페이지 다운시켜서 더 많은 이미지가 나오게 한다
for i in range(30):
body.send_keys(Keys.PAGE_DOWN)
time.sleep(0.5)#한번 드래그 후 sleep
imgs = driver.find_elements_by_css_selector('img.rg_i')
os.makedirs(keyword, exist_ok=True)
time.sleep(10)
for idx, img in enumerate(imgs):
# print(idx,img.get_attribute('src'))
imgUrl = img.get_attribute('src')
if imgUrl == None:
break
imgName = f'./{keyword}/{keyword+str(idx)}.jpg'
urllib.request.urlretrieve(imgUrl, imgName)