크롤링 오류 (사진을 다운을 못받아요)
조회수 524회
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait import time import urllib.request
search = 'keqing' # Double quotes for exact search term fileName = 'keqing' number = 500 interval = 1.0
driver = webdriver.Chrome() driver.get(f"https://www.google.com/search?q={search}&tbm=isch&hl=ko&tbs=il:ol&sa=X&ved=0CAAQ1vwEahcKEwjwlqOimdz5AhUAAAAAHQAAAAAQAg&biw=1249&bih=601")
Wait for the page to load
wait = WebDriverWait(driver, 10) # 최대 10초 기다림 wait.until(By.CSS_SELECTOR, '#islrg > div.islrc') # By.CSS_SELECTOR 추가
firstImage = driver.find_element(By.CSS_SELECTOR, '#islrg > div.islrc > div:nth-child(2) > a.wXeWr.islib.nfEiy > div.bRMDJf.islir > img') firstImage.click()
for i in range(number): try: time.sleep(interval)
# Wait for the image to be displayed
wait = WebDriverWait(driver, 10)
image = wait.until(By.CSS_SELECTOR, '#Sva75c > div > div > div.pxAole > div.tvh9oe.BIB1wf > c-wiz > div > div.OUZ5W > div.zjoqD > div.qdnLaf.isv-id.b0vFpe > div > a > img')
imageSrc = image.get_attribute('src')
urllib.request.urlretrieve(imageSrc, f'{fileName}_{i+1}.jpg')
print(f'{i+1}번째 이미지 저장 성공')
except Exception as e: # More specific exception handling
print(f'{i+1}번째 이미지 저장 오류 발생: {e}')
finally:
# Find the "Next" button using WebDriverWait
wait = WebDriverWait(driver, 10)
nextButton = wait.until(By.CSS_SELECTOR, '#Sva75c > div > div > div.pxAole > div.tvh9oe.BIB1wf > c-wiz > div > div.OUZ5W > div.zjoqD > div.mWagE.fDqwl > a:nth-child(4)')
nextButton.click()
driver.quit()
Traceback (most recent call last): File "d:\blueproject\1\selenium\app.py", line 17, in wait.until(By.CSS_SELECTOR, '#islrg > div.islrc') # By.CSS_SELECTOR 추가 File "C:\Users\REIMU\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\support\wait.py", line 96, in until value = method(self._driver) TypeError: 'str' object is not callable
댓글 입력