[Python Selenium] 파이썬 셀레늄 – (Colab) Linux Ubuntu 22.04 환경에서 셀레늄 실행

목차 구글 콜랩 셀레늄 환경설정셀레늄 테스트 구글 코랩 목차 구글 콜랩셀레늄 환경설정셀레늄 테스트 구글 코랩

구글 콜랩(Google Colab)은 구글이 제공하는 클라우드 기반 온라인 에디터다. 텍스트와 프로그램 코드를 자유롭게 작성할 수 있으며, 주피터 노트북(Jupyter Notebook) 에디터를 기반으로 어느 기기에서나 동일하게 제공되는 무료 GPU를 비롯한 환경에서 코딩이 가능하다. 콜랩 액세스 : https://colab.research.google.com/ ?hl=ko 콜랩 소개글 작성 : [Google Colab] Google 콜랩 스타트, GPU 무료사용 구글 콜랩(Google Colab)은 구글이 제공하는 클라우드 기반 온라인 에디터다. 텍스트와 프로그램 코드를 자유롭게 작성할 수 있으며, 주피터 노트북(Jupyter Notebook) 에디터를 기반으로 어느 기기에서나 동일하게 제공되는 무료 GPU를 비롯한 환경에서 코딩이 가능하다. 콜랩 액세스 : https://colab.research.google.com/ ?hl=ko 콜랩 소개글 작성 : [Google Colab] Google 콜랩 스타트, GPU 무료사용

그대로 입력하면 파이썬 코드를 실행시킬 수 있고, 를 입력하면 연결 중인 코랩 리눅스 우분투 환경에서 리눅스 명령을 실행한다. 다음 명령어는 현재 운영체제 정보를 포함하는 /etc/os-release 파일을 출력한 내용이다. 현재 접속된 코랩 환경은 데비안 계열, 리눅스 운영체제인 우분투 22.04.3 버전이다. 그대로 입력하면 파이썬 코드를 실행시킬 수 있고, 를 입력하면 연결 중인 코랩 리눅스 우분투 환경에서 리눅스 명령을 실행한다. 다음 명령어는 현재 운영체제 정보를 포함하는 /etc/os-release 파일을 출력한 내용이다. 현재 접속된 코랩 환경은 데비안 계열, 리눅스 운영체제인 우분투 22.04.3 버전이다.

!cat /etc/os-release !cat /etc/os-release

 

이 글에서 다루는 내용은 코랩에서의 파이썬 셀레늄 4버전 대 사용법을 다룹니다. 즉, CLI 환경에서의 Ubuntu 22.04 버전으로 셀레늄을 사용하는 방법입니다. 셀레늄 환경설정입니다 이 글에서 다루는 내용은 코랩에서의 파이썬 셀레늄 4버전 대 사용법을 다룹니다. 즉, CLI 환경에서의 Ubuntu 22.04 버전으로 셀레늄을 사용하는 방법입니다. 셀레늄 환경설정입니다

Windows 운영 체제에서 셀레늄을 사용하는 Windows 운영 체제에서 셀레늄 4 버전을 사용하려면 다음과 같이 Service()와 Chrome Driver Manager() 모듈을 사용하여 다운로드하여 설치한다. Windows 운영 체제에서 셀레늄을 사용하는 Windows 운영 체제에서 셀레늄 4 버전을 사용하려면 다음과 같이 Service()와 Chrome Driver Manager() 모듈을 사용하여 다운로드하여 설치한다.

 

반드시 적용해야 하는 옵션도 따로 없고 크롤링 방지 우회가 필요한 경우 user Agent를 설정해주는 것이 전부입니다. Linux: Chrome Driver 설치 최근 Linux Ubuntu 업데이트에서 Chrome Driver 설치 시 snap을 이용하도록 변경하고 snap 없이 설치하도록 패키지 관리자에게 저장소를 하나 추가해야 합니다. 다음과 같이 패키지 저장소에 해당 내용 없이 바로 리눅스용 크롬 드라이버인 chromium 드라이버를 설치하면 오류가 발생합니다. 반드시 적용해야 하는 옵션도 따로 없고 크롤링 방지 우회가 필요한 경우 user Agent를 설정해주는 것이 전부입니다. Linux: Chrome Driver 설치 최근 Linux Ubuntu 업데이트에서 Chrome Driver 설치 시 snap을 이용하도록 변경하고 snap 없이 설치하도록 패키지 관리자에게 저장소를 하나 추가해야 합니다. 다음과 같이 패키지 저장소에 해당 내용 없이 바로 리눅스용 크롬 드라이버인 chromium 드라이버를 설치하면 오류가 발생합니다.

# 크롬과 크롬 드라이버를 설치합니다! apt-get update! apt-get chromium-driver를 설치합니다 # 크롬과 크롬 드라이버를 설치합니다! apt-get update! apt-get chromium-driver를 설치합니다

 

주의 : ‘크롬 드라이버’가 아닌 ‘크롬-크롬 드라이버’를 선택하는 패키지 크롬은 사용할 수 없지만, 다른 패키지에 의해 참조되고 있습니다. 이것은 패키지가 누락되어 있거나, 폐지되어 있거나, 또는 다른 소스에서만 입수할 수 있음을 의미할지도 모릅니다. 그러나 다음 패키지가 대체됩니다. chromium-bsuE: 패키지 ‘chromium’에는 설치 후보가 없습니다 주의 : ‘크롬 드라이버’가 아닌 ‘크롬-크롬 드라이버’를 선택하는 패키지 크롬은 사용할 수 없지만, 다른 패키지에 의해 참조되고 있습니다. 이것은 패키지가 누락되어 있거나, 폐지되어 있거나, 또는 다른 소스에서만 입수할 수 있음을 의미할지도 모릅니다. 그러나 다음 패키지가 대체됩니다. chromium-bsuE: 패키지 ‘chromium’에는 설치 후보가 없습니다

따라서 패키지 관리자에 추가해야 하며 다음 명령으로 추가합니다. 코랩에서 한 줄의 리눅스 명령어는!을 각 앞에 붙여서 사용하는데 %% shell을 붙이면 그 코드블록 전체를 리눅스 셸 명령어로 인식합니다. 다음 명령어를 그대로 복사하여 실행하면 #의 주석부분을 제외한 리눅스 명령어를 실행합니다. 따라서 패키지 관리자에 추가해야 하며 다음 명령으로 추가합니다. 코랩에서 한 줄의 리눅스 명령어는!을 각 앞에 붙여서 사용하는데 %% shell을 붙이면 그 코드블록 전체를 리눅스 셸 명령어로 인식합니다. 다음 명령어를 그대로 복사하여 실행하면 #의 주석부분을 제외한 리눅스 명령어를 실행합니다.

# 이 부분은 처음 한번만 실행하면 됌.# 코드 수정 – “The reason is that the last Ubuntu update update supports chromium driver just via snap.”# 최근 우분투 업데이트에서 크롬 드라이버 설치를 snap을 이용해서만 하도록 바뀜# 고로 snap 없이 설치하는 아래 우회 코드로 변경# 출처 : https://colab.research.google.com/drive/1cbEvuZOhkouYLda3RqiwtbM-o9hxGLyC# 출처2 : https://stackoverflow.com/questions/75155063/selenium-use-chrome-on-colab-got-unexpectedly-exited%%shell# Ubuntu no longer distributes chromium-browser outside of snap## Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap# Add debian bustercat > /etc/apt/sources.list.d/debian.list <<‘EOF’deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster maindeb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates maindeb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates mainEOF# Add keysapt-key adv –keyserver keyserver.ubuntu.com –recv-keys DCC9EFBF77E11517apt-key adv –keyserver keyserver.ubuntu。com –recv-keys 648ACFD622F3D138apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 112695A0E562B32Aapt-key export 77E11517 | gpg –dearmour -o /usr/share/keyrings/debian-buster.gpgapt-key export 22F3D138 | gpg –dearmour -o /usr/share/keyrings/debian-buster-updates。gpgapt-key export E562B32A | gpg –dearmour -o /usr/share/keyrings/debian-security-buster.gpg# Prefer debian repo for chromium* packages only# Note the double-blank lines between entriescat > /etc/apt/preferences.d/chromium.pref << ‘EOF’Package: *Pin: release a=eoanPin-Priority: 500Package: *Pin: origin “deb.debian。org”Pin-Priority: 300Package: chromium*Pin: origin “deb.debian.org”Pin-Priority: 700EOF# Install chromium and chromium-driverapt-get updateapt-get install chromium chromium-driver # 이 부분은 처음 한번만 실행하면 됌.# 코드 수정 – “The reason is that the last Ubuntu update update supports chromium driver just via snap.”# 최근 우분투 업데이트에서 크롬 드라이버 설치를 snap을 이용해서만 하도록 바뀜# 고로 snap 없이 설치하는 아래 우회 코드로 변경# 출처 : https://colab.research.google.com/drive/1cbEvuZOhkouYLda3RqiwtbM-o9hxGLyC# 출처2 : https://stackoverflow.com/questions/75155063/selenium-use-chrome-on-colab-got-unexpectedly-exited%%shell# Ubuntu no longer distributes chromium-browser outside of snap## Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap# Add debian bustercat > /etc/apt/sources.list.d/debian.list <<‘EOF’deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster maindeb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates maindeb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates mainEOF# Add keysapt-key adv –keyserver keyserver.ubuntu.com –recv-keys DCC9EFBF77E11517apt-key adv –keyserver keyserver.ubuntu。com –recv-keys 648ACFD62

The cat > files << EOF> through EOF are the parts that enter and add the contents tied with EOF into the file as they are. The cat > files << EOF> through EOF are the parts that enter and add the contents tied with EOF into the file as they are.

Linux—Install a selenium library, i.e., a selenium library. Linux—Install a selenium library, i.e., a selenium library.

!pip install selenium !pip install selenium

selenium test selenium test

Now I’m going to write a Python script. Since windows cannot be displayed in Linux Ubuntu, which is a CLI environment, add the option headless and no-sandbox, which is a setting related to the Linux sandbox environment, to the option. Now I’m going to write a Python script. Since windows cannot be displayed in Linux Ubuntu, which is a CLI environment, add the option headless and no-sandbox, which is a setting related to the Linux sandbox environment, to the option.

SeleniumからWebドライバをインポートします。webdriver.common.by import By#Colab 에선 웹브라우저 창이 뜨지 않으므로 별도 설정한다.user_agent = “user-agent= mozilla/5.0 (Windows NT 10.0; Win64; x64) Apple WebKit/537.36 (KHTML、Gecko 同様) Chrome/118.0.0.0 Whale/3.23.214.10 Safari/537.36” オプション = Webdriver です。ChromeOptions()options.add_argument(user_agent)options.add_argument(‘–headless’)#ヘッドレス 설정options.add_argument(‘–no-sandbox’)options.add_argument(‘–disable-dev-shm-usage’)driver = Webdriver です。Chrome (options=options) です SeleniumからWebドライバをインポートします。webdriver.common.by import By#Colab 에선 웹브라우저 창이 뜨지 않으므로 별도 설정한다.user_agent = “user-agent= mozilla/5.0 (Windows NT 10.0; Win64; x64) Apple WebKit/537.36 (KHTML、Gecko 同様) Chrome/118.0.0.0 Whale/3.23.214.10 Safari/537.36” オプション = Webdriver です。ChromeOptions()options.add_argument(user_agent)options.add_argument(‘–headless’)#ヘッドレス 설정options.add_argument(‘–no-sandbox’)options.add_argument(‘–disable-dev-shm-usage’)driver = Webdriver です。Chrome (options=options) です

You can use it in this state from now on. You can use it in this state from now on.

#該当 url に移動url = “https://www.agoda.com/ko-kr/ ?site_id=1891463&tag=45b17d1d-e0b0-fe2a-ce90-5513829d856b&device=c&network=g&adid=576932895507&rand=5238650703878661146&expid=&adpos=&aud=kwd-6927948326 &gclid=Cjau kwca-04 Ahi Brna kwau #該当 url に移動url = “https://www.agoda.com/ko-kr/ ?site_id=1891463&tag=45b17d1d-e0b0-fe2a-ce90-5513829d856b&device=c&network=g&adid=576932895507&rand=5238650703878661146&expid=&adpos=&aud=kwd-6927948326 &gclid=Cjau kwca-04 Ahi Brna kwau

셀레늄 3 버전과 4 버전의 가장 큰 차이점은 find_element(By.<keyword>)를 사용하여 엘리먼트를 찾는다는 점입니다. 다음 부분에 유의하여 사용함. 셀레늄 3 버전과 4 버전의 가장 큰 차이점은 find_element(By.<keyword>)를 사용하여 엘리먼트를 찾는다는 점입니다. 다음 부분에 유의하여 사용함.

 

 

 

error: Content is protected !!