새벽을 밝히는 붉은 달

[Colab] Google Colab에서 Selenium 사용하기 본문

Develop/Python

[Colab] Google Colab에서 Selenium 사용하기

자윰 2023. 5. 13. 15:43

2년 전에  Colab에서 Selenium을 사용해서 크롤링을 했었는데, 이번에 크롤링을 할 일이 생겨 같은 코드를 동작시키니

 

WebDriverException: Message: Service /usr/bin/chromedriver unexpectedly exited. Status code was: 1

 

라며 크롤러를 동작시킬 수 없었다.

 

구글링을 통해 올해 1월에 올라온 글을 참고해봐도 같은 에러가 발생했다. 

결국엔 코랩 깃허브 레포의 이슈에서 해결방법을 찾았다. 아마 우분투 버전이 바뀌면서 그런 것 같다고 한다.

 

글 작성 시점 기준 (2023.05.13) Colab에서 Selenium을 동작시킬 수 있는 코드는 다음과 같다.

 

%%shell
sudo apt -y update
sudo apt install -y wget curl unzip
wget http://archive.ubuntu.com/ubuntu/pool/main/libu/libu2f-host/libu2f-udev_1.1.4-1_all.deb
dpkg -i libu2f-udev_1.1.4-1_all.deb
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
dpkg -i google-chrome-stable_current_amd64.deb
CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`
wget -N https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P /tmp/
unzip -o /tmp/chromedriver_linux64.zip -d /tmp/
chmod +x /tmp/chromedriver
mv /tmp/chromedriver /usr/local/bin/chromedriver
pip install selenium

 

위 코드를 한 번 실행해서 셀레니움을 설치한 후

 

from selenium import webdriver

URL = "https://google.com"

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--no-sandbox")

driver = webdriver.Chrome(
    options=options
)

driver.get(URL)
title = driver.title
driver.close()

print(title)

 

를 실행하면 Google이 출력되며 정상적으로 동작하는 것을 확인할 수 있다.

 

 

 

출처 : https://github.com/googlecolab/colabtools/issues/3347#issuecomment-1537274499

 

Issues when trying to use Chromedriver in Colab · Issue #3347 · googlecolab/colabtools

I have been running a program for months that uses Selenium in Google Colab. I have not had an issue with it until tonight. Each time I try to run the webdriver, I get the following error: I've als...

github.com

 

'Develop > Python' 카테고리의 다른 글

[Python] 백준 1427번: list() 에 대해 공부  (0) 2021.06.28
1. 나의 첫번째 Python 과제  (0) 2019.11.30
Comments