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
| keyword = input("검색할 키워드를 입력 : ") image_name = input("저장할 이미지(+폴더) 이름 : ")
if not os.path.exists(f'./{image_name}'): print(f'create directory ... {image_name}') os.mkdir(f'./{image_name}') driver = webdriver.Chrome(chrome_path) driver.get('http://www.google.co.kr/imghp?hl=ko') browser = driver.find_element(By.CSS_SELECTOR, 'body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input' ) browser.click() browser.send_keys(keyword) browser.send_keys(Keys.RETURN)
selenium_scroll_option() bb = driver.find_element(By.CSS_SELECTOR, '#islmp > div > div > div > div > div.gBPM8 > div.qvfT1 > div.YstHxe > input') bb.click() selenium_scroll_option()
images = driver.find_elements(By.CSS_SELECTOR, ".rg_i.Q4LuWd")
images_url = [] for i in images:
if i.get_attribute('src')!= None : images_url.append(i.get_attribute('src')) else : images_url.append(i.get_attribute('data-src'))
print("전체 다운로드한 이미지 개수: {}\n동일한 이미지를 제거한 이미지 개수: {}".format(len(images_url), len(pd.DataFrame(images_url)[0].unique()))) images_url=pd.DataFrame(images_url)[0].unique()
folder_name = (f'./{image_name}/') for i, url in enumerate(tqdm(images_url), 0): urlretrieve(url, folder_name + image_name + '_' + str(i) + '.jpg') driver.close()
|