TIL4(230118)_Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home 오류 해결하기

Jiwon In·2023년 1월 18일
0

TIL

목록 보기
4/4
post-thumbnail

홈페이지에 있는 내용을 스크래핑하기 위해 selenium을 사용하는데..
먼저 필요한 라이브러리를 불러오고

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
from tqdm import tqdm
import time

공동소송을 진행하는 기업 홈페이지에서 공동소송 프로젝트와 날짜, 해당 공동소송을 진행한 변호사 이름을 긁어오기 위한 코드를 작성하고 실행을 하면
tmp_list=[]
driver=webdriver.Chrome('chromedriver.exe')
error_cnt=0
for i in tqdm(range(0, 142)):
    url='https://prj.angrypeople.co.kr/progress/v/'+str(i)
    try:
        driver.get(url)
        html=driver.page_source
        soup=BeautifulSoup(html, 'html.parser')
		# 프로젝트 이름, 날짜, 변호사이름
        project_name=soup.select('div.title.mgt-2')[0].text.strip()
        project_date=soup.select('div.date.mgt-2')[0].text.strip()
        lawyer_name=soup.select('div.inner-name')[0].text.strip()
        tmp_list.append([project_name, project_date, lawyer_name])
        time.sleep(0.5)
    except: 
        error_cnt=error_cnt+1
df=pd.DataFrame(tmp_list)
df.columns=['프로젝트명','날짜','변호사명']
print(error_cnt)


이런 오류가 뜬다..

WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

직관적으로 해석하면 경로에 없다는 건데..아무리 구글에 서치를 해서 뜨는 해결방법을 써도 해결되지 않았다.


시도한 방법 1. webdriver.Chrome()호출 시 절대경로 지정 → 실패

driver = webdriver.Chrome(executable_path='C:/python/chromedriver_win32/chromedriver.exe')

시도한 방법 2. 크롬브라우저 버전 확인 후 버전에 맞는 chromedriver 다운 받기 → 실패

크롬 브라우저가 그세 업그레이드되어 chromedriver의 버전이 맞지 않아 못 찾는 것 같아 버전에 맞게 새로 다운받았는데 이것도 문제가 아니었음


시도한 방법 3. 경로 변경 → 실패

chromedriver 파일 실행하고자 하는 python파일이 있는 폴더 안에다 넣어주라고 해서 넣었는데도 실패


시도한 방법 4. 라이브러리를 추가로 불러온 후, 코드 추가 작성 → 성공

추가로 필요한 라이브러리

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import pandas as pd
from tqdm import tqdm
import time, random, datetime

추가 코드

def chromeWebdriver():
    chrome_service = ChromeService(executable_path=ChromeDriverManager().install())
    options = Options()
    options.add_experimental_option('detach', True)
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'
    options.add_argument(f'user-agent={user_agent}')
    driver = webdriver.Chrome(service=chrome_service, options=options)
    return driver

이전에 바늘이야기와 프로젝트를 진행하면서 네이버쇼핑 리뷰를 스크래핑할 때 썼던 코드인데, 아무리 경로를 옮겨도 해결되지 않는다면 이 코드를 활용해보자..

profile
데글데글 굴러가는 데린이 일지

0개의 댓글