구글에서 사진 다운로드 받기

김승환·2021년 7월 27일
0

딥러닝공부

목록 보기
11/17

구글에서 사진 다운로드 받기

아래 패키지가 없는 분은 먼저 다운 해주세요

pip install beautifulsoup4
pip install selenium
pip install requests

코드는 아래 링크에서 이해하고 따라하면 되겠습니다.

크롬드라이버 다운로드를 먼저 진행해주세욥

한번 검색에 하나의 사진만 저장하게 했으며 이름이 중복인 경우 그냥 덮어쓰기로 됩니다.

사용방법

  • 코드내에서 크롬드라이버 설치 경로 설정
  • 코드내에서 다운로드 받을 폴더 설정
  • 코드실행 후 원하는 키워드 입력
  • 이후 자동 저장이 되며 반복됩니다.

#아래 패키지가 모두 실행 되면 크롤링 됩니다!
from urllib.request import urlretrieve
from urllib.parse import quote_plus    
from bs4 import BeautifulSoup as BS    
from selenium import webdriver

필요한 각 경로를 설정해주세요

#크롬드라이버 설지한 경로
CromeDriver = 'C:/chromedriver_win32/chromedriver.exe'

#다운로드 받을 위치 경로
DownloadPath = 'C:/Users/Desktop/picture/download'
while True:
    keyword = input("Image Name : ")
    if keyword == 'stop':
        break
    i_URL = f'https://www.google.com/search?q={quote_plus(keyword)}&sxsrf=ALeKk00OQamJ34t56QSInnMzwcC5gC344w:1594968011157&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjXs-7t1tPqAhVF7GEKHfM4DqsQ_AUoAXoECBoQAw&biw=1536&bih=754'
    
    driver=webdriver.Chrome(CromeDriver) #크롬 드라이버 다운 위치경로
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    driver.get(i_URL)
    
    html = driver.page_source
    soup = BS(html,features="html.parser")

    img = soup.select('img')
    
    html = driver.page_source
    soup = BS(html,features="html.parser")

    img = soup.select('img')

    i_list = []


    print("Searching...")
    for i in img:
       try:
          i_list.append(i.attrs["src"])
       except KeyError:
          i_list.append(i.attrs["data-src"])

    print("Downloading...")
    for i in i_list:
       urlretrieve(i,"download/"+keyword+".jpg")
       break

    driver.close()
    print("FINISH")
    print('검색 중지는 -->  stop')
    print('---------------------------------------')

Image Name : 헤이즈
Searching...
Downloading...
FINISH
검색 중지는 --> stop

profile
인공지능 파이팅!

0개의 댓글