홈페이지에 있는 내용을 스크래핑하기 위해 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
직관적으로 해석하면 경로에 없다는 건데..아무리 구글에 서치를 해서 뜨는 해결방법을 써도 해결되지 않았다.
driver = webdriver.Chrome(executable_path='C:/python/chromedriver_win32/chromedriver.exe')
크롬 브라우저가 그세 업그레이드되어 chromedriver의 버전이 맞지 않아 못 찾는 것 같아 버전에 맞게 새로 다운받았는데 이것도 문제가 아니었음
chromedriver 파일 실행하고자 하는 python파일이 있는 폴더 안에다 넣어주라고 해서 넣었는데도 실패
추가로 필요한 라이브러리
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
이전에 바늘이야기와 프로젝트를 진행하면서 네이버쇼핑 리뷰를 스크래핑할 때 썼던 코드인데, 아무리 경로를 옮겨도 해결되지 않는다면 이 코드를 활용해보자..