[python] crawler_google image
2021. 5. 18. 00:16ㆍPython/코딩
Ver. Jupyter Notebook (Anaconda3)
▶ crawler_google image
수집: 사진
코딩: github
JeongJaeyoung0/crawler
Contribute to JeongJaeyoung0/crawler development by creating an account on GitHub.
github.com
2021.05.18
# crawler_youtube image
Step 1. 구글 검색 > 이미지 > 이미지 저장
pwd
### step 0. 준비
import pandas as pd
import numpy as np
import os
import urllib.request
from selenium import webdriver
from tqdm import tqdm
import time
from time import sleep
### step 1. 이미지 크롤링
# 크롤링 할 검색어, 이미지 개수
keyword = input("크롤링할 검색어 입력: ")
image_num = int(input("크롤링할 이미지 개수: "))
%%time
# 폴더 생성
if not os.path.exists("image_google_"+keyword):
os.makedirs("image_google_"+keyword)
# 크롬창 띄우기
driver = webdriver.Chrome(r"G:\내 드라이브\exe\chromedriver.exe")
driver.get("https://www.google.com/search?q=%s"%keyword)
time.sleep(1)
# 이미지 클릭
driver.find_element_by_css_selector(".hide-focus-ring").click()
# 첫 번째 사진 클릭
driver.find_element_by_xpath("/html/body/div[2]/c-wiz/div[3]/div[1]/div/div/div/div/div[1]/div[1]/div[1]/a[1]/div[1]/img").click()
count = 0
for i in range(image_num):
# 이미지 URL get
imgURL = driver.find_element_by_xpath("/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div/div[2]/a/img").get_attribute("src")
# 이미지 저장 (파일명 중복 확인 중복이면 count 1씩 증가)
swich = 'on'
while swich == 'on':
if not os.path.exists("image_google_{0}/image_google_{0}_{1} ({2}).jpg".format(keyword, count, i)):
urllib.request.urlretrieve(imgURL, "image_google_{0}/image_google_{0}_{1} ({2}).jpg".format(keyword, count, i))
swich = 'off'
else: count += 1
# 다음 이미지
driver.find_element_by_xpath("/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div/div[1]/a[2]/div").click()
time.sleep(0.5)
driver.close()
'Python > 코딩' 카테고리의 다른 글
[wordcloud] naver cafe_게시판 글 목록 (0) | 2021.05.20 |
---|---|
[crawler] naver cafe_게시판 글 목록 (0) | 2021.05.19 |
[crawler] youtube(selenium) (1) | 2021.05.17 |
[wordcloud] kakao_talk (0) | 2021.05.15 |
[python] crawler_naver 부동산 (0) | 2021.05.14 |