Colab에서 Selenium을 실행하는 현재(2023년12월)기준 최신판입니다. 작동이 안되는 정보가 많아 간단하게 정리하였습니다.
Colab, Selenium관련 라이브러리 설치
!pip install selenium
!apt-get update
!apt-get install chromium chromium-driver
- !apt-get update: 운영체제에서 사용 가능한 패키지들과 그 버전에 대한 정보를 업데이트하는 명령이다. 설치되어 있는(Installed) 패키지를 업데이트 하는 것이 아니라 설치 가능한(Available) 리스트를 업데이트 합니다.
Code Block
def web_driver():
options = webdriver.ChromeOptions()
options.add_argument("--verbose")
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("--window-size=1920, 1200")
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
return driver
import selenium
import bs4
from selenium.webdriver.common.by import By
driver = web_driver()
driver.get('https://www.daum.net/')
driver.find_element(By.XPATH, '//*[@id="q"]').send_keys('고려대학교')
driver.find_element(By.XPATH, '//*[@id="daumSearch"]/fieldset/div/div/button[3]').click()
driver.find_element(By.XPATH, '//*[@id="daumGnb"]/div/ul/li[7]/a').click()
bsObj = bs4.BeautifulSoup(driver.page_source, "html.parser")
image_url = bsObj.find("div", {"class":"cont_image"}).img.attrs['src']
driver.quit()
- “다음”에서 “고려대학교”를 검색 후 클릭하는 Selenium예시로 중요한 것은 Colab에서 Selenium을 실행하기 위해서는 “options.add_argument(‘–headless’)” 필수
- Good Luck~