본문 바로가기

파이선 (python)

[Python] 웹크롤링 시 불필요한 콘솔 메시지 제거하기

728x90

[ 불편사항 ]

* python으로 웹 크롤링을 구현하였을 때 콘솔창이 뜨게 되는데 이 콘솔에 표시되는 복잡한 로그성 메시지들이 보인다.

* 문제는 이 로그성 메시지들이 내가 필요한 정보라기보다는 시스템적인 내용이 대부분이라는 것이다.

* 프로그램 작성 오류나 워닝 메시지일 경우는 도움이 되는데 개발자와 상관없는 내용들이기 때문에 불필요한 메시지일 뿐이다.

* 아래의 화면에서 빨간색으로 표시해 둔 메시지들이 여기에 해당된다.

 

[ 해결방법 ]

* 셀레니움 모듈에서 웹드라이버를 선언할 때 Chrome 드라이버의 옵션을 다루는 Options 클래스를 import 한 후,

* 불필요한 콘솔 메시지를 제거하도록 설정하면 아래와 같은 불필요한 시스템 메시지들이 표시되지 않는다.

 

* 해당 소스코드는 아래에 변경 전/후 구분하였고, 밑줄 그은 부분만 참고하면 된다.

 

<변경전>

from selenium import webdriver
from selenium.webdriver.common import keys
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

 

options = webdriver.ChromeOptions()

browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

browser.get('https://www.google.co.kr/')

 

<변경후>

from selenium import webdriver
from selenium.webdriver.common import keys
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

from selenium.webdriver.chrome.options import Options       # Chrome 브라우저 Options 클래스를 가져온다.

from webdriver_manager.chrome import ChromeDriverManager

 

options = webdriver.ChromeOptions()

options.add_experimental_option('excludeSwitches', ['enable-logging'])   # 불필요한 콘솔 메시지를 제거한다.

browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

browser.get('https://www.google.co.kr/')