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