from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd
driver=webdriver.Chrome(service=Service(ChromeDriverManager().install()))
result = pd.DataFrame()
page_no = 1
while page_no < 15:
page_url="https://teacher.i-scream.co.kr/course/crs/creditView.do?searchOrdinalTyCode=TY01&crsListType=CREDIT&searchCrsLCode=&searchCredit=&searchCondition=&searchKeyword=&crsCode=1191&pageIndex={}&sso=ok#tab3".format(page_no)
title = []
content = []
driver.get(page_url)
for i in range(3,13):
a=driver.find_element(By.XPATH,f'//*[@id="tab3"]/div[1]/div[2]/dl/dd[{i}]').get_attribute('innerText')
b=driver.find_element(By.XPATH,f'//*[@id="tab3"]/div[1]/div[2]/dl/dt[{i}]/em[1]/a').get_attribute('innerText')
content.append(a)
title.append(b)
data = pd.DataFrame({"title":title, "content":content })
result = pd.concat([result,data])
page_no+=1
result['course']='조병영의 문해력수업'
result.to_excel('./조병영의 문해력 수업.xlsx', index=False, encoding = 'utf8')
위 사이트 (아이스크림 연수원) 리뷰의 제목을 눌러야 내용이 나오는 형태로 되어있었다. 클릭하지 않는다면 style=none의 형태로 되어있어 class를
기반으로 크롤링을 시행하면 아무런 값이 출력되지 않았다. 그러므로 XPATH를 사용하였고
.get_attribute('innerText')를 사용하여 모든 텍스트를 가져와 자료를 수집하였다. 위와 같은 방법을 반복하여 총 6천 개 정도의 자료를 수집하였다
페이지는 1을 디폴트 값으로 두었고,
while문을 기준으로 특정값 까지 반복해서 실행하였다.
best리뷰의 갯수에 따라 페이지당 총 리뷰수가 결정되었기에
xpath값으로 지정하기에 한번에 코드를 설정하기에 무리가 있었다.
그래서 각각 하나씩 크롤링을 실시하였다.