MacOs기준

Step1. Python가상환경 설정

  1. python venv 가상환경
  2. miniforge3 conda 가상환경
    가상환경을 만드는 2가지 방법 중 python venv를 이용
    • python venv 가상환경 설정 방법.
python -m venv /path/to/new/virtual/environment ## 가상환경 설정

ex) python -m venv Crawling
cd Crawling/Scripts # 가상환경 폴더로 이동 후
activate ## activate 명령어 사용

Step3. Selenium 설치

pip install selenium

Chrome Driver 설치

  • 사용중인 Chrome 버전 확인 버전 99.0.4844.51(공식 빌드) (x86_64)
  • Chromedriver 검색 후 버전에 맞게 설치
  • 압축 해제 후 chromedriver을 크롤링 코드와 같은 폴더로 이동
  • 코드 작성
## 기본코드
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome("Crawling/chromedriver") # driver가 저장된 상대경로
driver.get("https://www.google.co.kr")

빠른 웹 이미지 크롤링 icrawler

  • 기존 크롤링보다 훨씬 더 빠른 속도로 이미지를 수집할 수 있다 이미지 크롤링에 특화되어있다.
pip install icrawler

위 명령어로 라이브러리를 설치 이후

from icrawler.builtin import GoogleImageCrawler
import os

# Google에서 크롤링
# 이미지 저장 폴더 경로
save_dir = os.path.join('./save/') ##원하는경로를 설정
# GoogleImageCrawler 객체 생성
filters = {
    'size': 'large',
    'license': 'noncommercial,modify', ## 비상업 , 수정가능 옵션 추가
    'color': 'blackandwhite'
    }

google_crawler = GoogleImageCrawler(storage={'root_dir': save_dir})
google_crawler.crawl(keyword='Dog',min_size = (200,200),
                     max_num=50, filters=filters)

상당히 빠른 속도로 크롤링이 가능하다.