Development/Linux

[Python] 리눅스 서버(centos7)에 파이썬 크롤러 환경 셋팅하기

곽진돔 2023. 1. 26. 17:36

윈도우에서 테스트용으로 작성한 파이썬 크롤러를 리눅스에도 적용 시키려고 한다.

python 버전 업그레이드

현재 centOS7환경을 사용 중이며, 기본적으로 python 2 버전대를 포함하고 있기 때문에 3으로 업그레이드한다.

yum install python3.6

업그레이드는 아래 포스팅 참고👇🏻

https://dev-wisdom.tistory.com/141

 

[centOS7] Python 버전 업그레이드하기

centos7에는 기본적으로 Python 2.7.5 (default, Oct 14 2020, 14:45:30)가 설치되어있다. 현재 사용중인 python 코드의 버전은 3.7.9이여서 버전 업그레이드를 했다. # python -v Python 2.7.5 (default, Oct 14 2020, 14:45:30) # y

dev-wisdom.tistory.com

사용하는 모듈 설치하기

-bash: pip: command not found가 뜰 경우

필요한 라이브러리 모듈을 설치한다. (쉘에서 pip install)

yum install epel-release
yum install python-pip

개발 도구 설치

pip --version
yum install python-devel

requests 설치

HTTP 라이브러리

pip install requests

위의 에러가 발생할 경우

 Could not find a version that satisfies the requirement requests (from versions: )
No matching distribution found for requests가 발생할 경우 : openssl 버전 문제 1.0.1/1.0.2 지원안됨

1.0.1은 지원 중단, 

selenium & chromedriver 설치

웹 애플리케이션 자동화 및 테스트 프레임워크 & chrome

pip install selenium
pip install undetected_chromedriver

selenium과 chromde driver 설치 후 버전 에러가 날 경우, (버전이 일치해야 실행 가능)

크롬브라우저 다운로드 사이트에서 해당 버전으로 다운로드 후 크롬드라이버 파일이 사용하는 위치에 복사하기.

또는 webdriver-manager를 설치한다. webdriver-manager 설치시 항상 최신 버전의 chromedriver를 자동으로 사용할 수 있다.

# pip install webdriver-manager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriber.Chrome(ChromeDriverManager().install())

#options 사용할 경우
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)

beautifulsoup4 설치

pip install bs4

pymysql 설치

pip install pymysql

에러 발생

Traceback (most recent call last):
  File "/home/metrix/buzzmetrix/python/360_seed_source_data_URL.py", line 13, in <module>
    import undetected_chromedriver as uc
  File "/usr/local/lib/python3.6/site-packages/undetected_chromedriver/__init__.py", line 31, in <module>
    from . import v2
  File "/usr/local/lib/python3.6/site-packages/undetected_chromedriver/v2.py", line 4
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined

 

찾아보니 윈도우에서는 GUI환경이여서 화면을 볼 수 있다. 크롬 드라이버를 사용하여 실제 창을 띄우고, 그 창을 볼 수 있다.

(리눅스도 GUI버전으로 설치하면 가능하다)

CLI로 설치하면 kernel만 있고 화면은 없어서 디스플레이를 띄울 수 없다. 그래서 CLI에서 셀레니움을 사용하려면 방법이 조금 다르다.

브라우저를 이용할 때, 기본적으로 창이 실행되면서 불러와지는 화면을 브라우저가 자동으로 표시해준다. 하지만 서버에서는 일반적으로 화면 자체가 존재하지 않기때문에 일반적인 방식으로는 크롬을 사용할 수 없다. 이를 해결하기 위해 headless 옵션을 사용한다. 브라우저 창을 실제로 운영체제의 창으로 띄우지않고 화면을 그려주는 작업(렌더링)을 가상으로 진행하는 방법으로, 실제 브라우저와 동일하게 동작하지만 창은 뜨지않는 방식이다.

selenium은 web browser를 화면에 띄우고, 거기서 html 데이터를 가지고 온다. 하지만 디스플레이가 없으면 selenium을 이용한 웹 크롤러는 동작하지 않는다.

=> chromedriver를 활용해서 headless web crawler를 개발한다.

크롬드라이버 설치 후 코드를 추가해주면 될 것 같다.

크롬드라이버 설치하기

(작업 전)

참고

https://lsjsj92.tistory.com/500

 

linux(centos 7)에서 chrome driver로 headless 웹 크롤러(web crawler) 개발하기

가끔 웹 크롤러를 개발하다 보면 이런 상황이 있습니다. 윈도우에서 web crawler를 개발하면 당연히 화면이 있죠? 여기서 화면이라는 것은 window가 있다는 것입니다. 즉, 크롬 브라우저를 띄울 수 있

lsjsj92.tistory.com

https://beomi.github.io/2017/09/28/HowToMakeWebCrawler-Headless-Chrome/

 

나만의 웹 크롤러 만들기(7): 창없는 크롬으로 크롤링하기 - Beomi's Tech blog

2017-09-28 나만의 웹 크롤러 만들기(7): 창없는 크롬으로 크롤링하기 좀 더 보기 편한 깃북 버전의 나만의 웹 크롤러 만들기가 나왔습니다! 이번 가이드는 가이드 3편(Selenium으로 무적 크롤러 만들

beomi.github.io

https://stackoverflow.com/questions/60296873/sessionnotcreatedexception-message-session-not-created-this-version-of-chrome

 

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81

I am currently new to robot framework.I am currently using latest window version of chrome and chromedriver which is 80 but when i try to run the test it gives the message "SessionNotCreatedExcepti...

stackoverflow.com