네이버 이미지 selenium

Happy_JG·2023년 10월 10일
0

크롤링

목록 보기
8/8
from selenium import webdriver as wb
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys

# 파일시스템을 위한 라이브러리(삭제, 생성)
import os
# 이미지의 경로를 실제 파일로 저장
from urllib.request import urlretrieve
# 폴더 생성
# 바탕화면에 이미지라는 폴더가 없다면 바탕화면에 이미지라는 폴더를 만들어라
if not os.path.isdir("C:/Users/gjaischool/Desktop/dove") :
    os.mkdir("C:/Users/gjaischool/Desktop/dove")
driver = wb.Chrome()
driver.get('https://search.naver.com/search.naver?sm=tab_hty.top&where=image&query=%EB%8B%A4%EB%9E%8C%EC%A5%90&oquery=%EC%B0%B8%EC%83%88&tqi=iffk5wp0J1Zss6d3M2wssssss9K-437096')
# 스크롤 하는코드
for i in range(10) :
    body = driver.find_element(By.CSS_SELECTOR,"body")
    body.send_keys(Keys.END)
# 이미지 태그들을 수집
img = driver.find_elements(By.CSS_SELECTOR,"._image._listImage")
# 태그 안에있는 속성을 가지고 올 때는 get_attribute("속성")
img[0].get_attribute("src")
# src 만 담아줄 리스트를 제작
src = []

for i in range(len(img)) :
    src.append(img[i].get_attribute("src"))
# 이미지의 경로를 실제 파일로 저장
for i in range(len(src)) :
    urlretrieve(src[i],"C:/Users/gjaischool/Desktop/dove/"+"dove"+str(i)+".jpg")
profile
hello!

0개의 댓글