Post

우분투 서버에서 크롤링(1)

우분투 서버에서 크롤링(1)

크롤링을 우분투 서버에서 작동시켜야한다 따라서 우분투에서 크롤링을 테스트 해본다 크롬으로도 가능하지만 드라이버와 버전 맞추기가 귀찮아 기본적으로 설치되어 있는 파이어 폭스를 이용한다

 

Geckodriver

geckodriver download 여기서 자신의 버전에 맞는 드라이버를 설치한다

wget https://github.com/mozilla/geckodriver/releases/download/v0.34.0/geckodriver-v0.34.0-linux64.tar.gz –> 34버전 다운로드

tar -zxvf [파일명.tar.gz]를 통해서 압축해제

이후 geckodriver를 각자 컴퓨터 위치에 맞게 재설정 (mv를 이용해서 이동함) /usr/bin으로 이동했다

 

파이썬 selenium 설치 및 크롤링 코드

` pip install selenium` 셀레니움 설치

pip install webdriver-manager 웹드라이버 매니저 설치

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 from selenium import webdriver
 from selenium.webdriver.firefox.service import Service
 from selenium.webdriver.firefox.options import Options as FirefoxOptions
 
 # Firefox 옵션 설정 (예: headless 모드)
 options = FirefoxOptions()
 options.add_argument("--headless")  # GUI 없이 실행
 
 # Geckodriver 서비스 설정
 service = Service(executable_path="/usr/bin/geckodriver") #드라이버 위치
 
 # Firefox 웹 드라이버 인스턴스 생성
 driver = webdriver.Firefox(service=service, options=options)
 
 # 웹 페이지 열기
 driver.get("https://www.google.com")
 
 # 페이지 전체 출력
 print(driver.page_source)
 
 # 브라우저 닫기
 driver.quit()
 

파이어 폭스 재설치(snap -> apt)

우분투 서버에 기본적으로 파이어 폭스가 존재하지만 snap으로 설치되어있다

코드를 실행시켜보니 오류 발생

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
User
Traceback (most recent call last):
  File "/home/minjeong/crawlingtest/crawling2.py", line 12, in <module>
    browser = webdriver.Firefox(service=webdriver_service, options=opts)
  File "/home/minjeong/.local/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 69, in __init__
    super().__init__(command_executor=executor, options=options)
  File "/home/minjeong/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__
    self.start_session(capabilities)
  File "/home/minjeong/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
  File "/home/minjeong/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "/home/minjeong/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Failed to read marionette port

이 오류를 해결하는 방법은 snap으로 설치된 firefox를 apt로 설치해서 해결했다

삭제와 재설치 방법 출처

  1. 기존 snap에 설치된 파이어 폭스 삭제

sudo snap remove firefox

  1. sudo install -d -m 0755 /etc/apt/keyrings

  2. wget -q https://packages.mozilla.org/apt/repo-signing-key.gpg -O- | sudo tee /etc/apt/keyrings/packages.mozilla.org.asc > /dev/null

  3. echo "deb [signed-by=/etc/apt/keyrings/packages.mozilla.org.asc] https://packages.mozilla.org/apt mozilla main" | sudo tee -a /etc/apt/sources.list.d/mozilla.list > /dev/null

  4. 1
    2
    3
    4
    5
    
    echo '
    Package: *
    Pin: origin packages.mozilla.org
    Pin-Priority: 1000
    ' | sudo tee /etc/apt/preferences.d/mozilla
    
  5. sudo apt update && sudo apt install firefox

    변경후 파이썬 코드는 정상적으로 작동한다

     

This post is licensed under CC BY 4.0 by the author.