발생에러: urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)>
검색어 '고양이' 경우에 위의 에러가 떴습니다.
import ssl #_ssl.c:997
ssl._create_default_https_context = ssl._create_unverified_context # _ssl.c:997 에러처리
그래서 위의 문구를 검색해서 추가해줬지만,
여전히, urllib.error.URLError: <urlopen error [SSL: WRONG_SIGNATURE_TYPE] wrong signature type (_ssl.c:997)>에러가 뜹니다.
결국, try로 에러를 처리했는데요. try없이 에러 조치가 가능할까요?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import os
import urllib.request
import ssl #_ssl.c:997
import pyautogui
keyword = pyautogui.prompt()
if not os.path.exists(f'06_구글이미지크롤링/{keyword}'):
os.mkdir(f'06_구글이미지크롤링/{keyword}')
main_url = f"https://www.google.com/search?q={keyword}&newwindow=1&tbm=isch&sa=X&ved=&biw=1000&bih=900&dpr=1"
browser = webdriver.Chrome("C:/chromedriver.exe")
browser.implicitly_wait(10)
browser.maximize_window()
browser.get(main_url)
#무한 스크롤 처리
before_h = browser.execute_script("return window.scrollY")
while True:
browser.find_element_by_css_selector("body").send_keys(Keys.END)
time.sleep(1)
after_h = browser.execute_script("return window.scrollY")
if after_h == before_h:
break
before_h = after_h
imgs = browser.find_elements_by_css_selector(".rg_i.Q4LuWd") #자료형은 리스트
for i, img in enumerate(imgs, 1):
browser.execute_script("arguments[0].click();", img)
time.sleep(1)
if i == 1:
target = browser.find_elements_by_css_selector("img.n3VNCb")[0]
else:
target = browser.find_elements_by_css_selector("img.n3VNCb")[1]
img_src = target.get_attribute('src')
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'Mozila/5.0')]
urllib.request.install_opener(opener)
ssl._create_default_https_context = ssl._create_unverified_context # _ssl.c:997 에러처리
try:
urllib.request.urlretrieve(img_src, f'06_구글이미지크롤링/{keyword}/{i}.jpg')
except:
continue