본문 바로가기
Dev metacog/DevCaJournal

img crawl 개발일지

by 잘 배우고, 잘 익히기 2021. 9. 14.

개발 흐름 

 - 필요한 패키지 import : dload(그림저장), BeautifulSoup(웹읽기), webdriver(브라우저기동), time(sleep동작) 

핵심 스킬

 - 브라우저의 '검사' 기능 이용 copy>selector 사용 반복부 찾기 :  thumnails = soup.select('#imgList > div > a > img')

 - 저장할 폴더를 포맷팅 기능으로 적용 : dload.save(img, f'imgs_homework/{i}.jpg')

 - 구글 webdriver : https://chromedriver.storage.googleapis.com/index.html?path=93.0.4577.63/

import dload
from bs4 import BeautifulSoup
from selenium import webdriver
import time

driver = webdriver.Chrome('chromedriver') # 웹드라이버 파일의 경로
driver.get("https://search.daum.net/search?nil_suggest=btn&w=img&DA=SBC&q=%EC%88%98%EC%A7%80")
time.sleep(5) # 5초 동안 페이지 로딩 기다리기

req = driver.page_source
# HTML을 BeautifulSoup이라는 라이브러리를 활용해 검색하기 용이한 상태로 만듦
# soup이라는 변수에 "파싱 용이해진 html"이 담긴 상태가 됨
# 이제 코딩을 통해 필요한 부분을 추출하면 된다.
soup = BeautifulSoup(req, 'html.parser')

###################################
# 이제 여기에 코딩을 하면 됩니다!
###################################

thumnails = soup.select('#imgList > div > a > img')
#imgList > div:nth-child(1) > a > img
i = 1
for thumnail in thumnails:
    img = thumnail['src']
    dload.save(img, f'imgs_homework/{i}.jpg')
    i += 1



driver.quit() # 끝나면 닫아주기

 

 

'Dev metacog > DevCaJournal' 카테고리의 다른 글

word cloud 개발일지  (0) 2021.09.16
web crawl 개발일지  (0) 2021.09.15