Skip to content

Instantly share code, notes, and snippets.

@Corikachu
Created January 9, 2017 09:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Corikachu/8b36c4fec48f6d43870e42137a162126 to your computer and use it in GitHub Desktop.
Save Corikachu/8b36c4fec48f6d43870e42137a162126 to your computer and use it in GitHub Desktop.
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()
@JaehyunAhn
Copy link

우왕 굿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment