Created
January 9, 2017 09:37
-
-
Save Corikachu/8b36c4fec48f6d43870e42137a162126 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
from selenium import webdriver | |
import requests | |
import shutil | |
import os | |
url = 'https://www.google.co.kr/search?q={search}&tbm=isch' | |
search_keyword = '트와이스 모모' | |
now_path = os.getcwd() | |
now_path = os.path.join(now_path, search_keyword) | |
os.makedirs(now_path, exist_ok=True) | |
driver = webdriver.Chrome() | |
driver.get(url.format(search=search_keyword)) | |
elem_list = driver.find_elements_by_css_selector('.rg_ic.rg_i') | |
for elem in elem_list: | |
elem.click() | |
particle_items = driver.find_elements_by_css_selector('.irc_fsl.irc_but') | |
image_url = '' | |
for particle_item in particle_items: | |
if particle_item.get_attribute('href') and particle_item.is_displayed(): | |
image_url = particle_item.get_attribute('href') | |
break | |
if not image_url: | |
continue | |
title_item = driver.find_elements_by_css_selector('._Epb.irc_tas') | |
filename = os.path.join(now_path, str(number) + title_item[2].text + '.png') | |
print(image_url) | |
response = requests.get(image_url, stream=True) | |
if response.status_code == 200: | |
try: | |
with open(filename, 'wb') as file: | |
response.raw.decode_content = True | |
shutil.copyfileobj(response.raw, file) | |
except Exception: | |
pass | |
driver.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
우왕 굿