TripAdvisor에서 장생건강원이라는 식당의 댓글을 크롤링해보겠습니다.
Jangseng Geongangwon
23, Gangnam-daero 124-gil, Gangnam-gu, Seoul 06114, South Korea
www.tripadvisor.com
홈페이지 살펴보기
트립어드바이저 댓글은 처음에는 앞부분만 표시되고 나머지부분은 표시가 안되어있다.
나머지부분을 보려면 더보기 버튼을 눌러야한다.
![[Crawling] TripAdvisor 댓글 크롤링 0](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
![[Crawling] TripAdvisor 댓글 크롤링 1](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
더보기 버튼을 누르면 숨기기 버튼으로 바뀌고 숨겨진 부분들이 표시됩니다.
따라서 크롤링 하기 전에 이 더보기 부분을 눌러서 나머지 부분이 보이게 한다음에 크롤링을 해야 합니다.
트립어드바이저 페이지 소스 살펴보기
![[Crawling] TripAdvisor 댓글 크롤링 2](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
- 더보기버튼 : span.taLnk.ulBlueLinks(전체 더보기 버튼)
- 댓글 : p.partial_entry
url
url = 'https://www.tripadvisor.co.kr/Restaurant_Review-g294197-d17423735-Reviews-Jangseng_Geongangwon-Seoul.html'
주요 라이브러리를 import하고 크롬 드라이버 실행
import time
from bs4 import BeautifulSoup
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = Chrome()
driver.set_window_size(1920,1080) #반응형 웹의 경우 size를 고정시키는 것이 좋다.
driver.get(url)
더보기 버튼
# more_btns = driver.find_elements_by_css_selector('span.taLnk.ulBlueLinks') -> 실행되기 전에 100%창이 뜬다는 확신이 없으면 안쓰는게 좋음
more_btns = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR,'span.taLnk.ulBlueLinks'))
) #생길때까지 10초간 기다린다, 만약5초만에 생기면 바로 넘어간다.
more_btns[0].click()
댓글 로딩 및 크롤링
#댓글이 모두 로딩될때까지 대기.
time.sleep(1)
html_doc = driver.page_source
driver.close()
soup = BeautifulSoup(html_doc)
comments = []
comment_tags = soup.select('p.partial_entry')
for tag in comment_tags:
comments.append(tag.text.strip())
#결과
![[Crawling] TripAdvisor 댓글 크롤링 3](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
페이징 처리
- 한 페이지의 댓글을 읽은 뒤(1. 더보기 버튼 클릭, 2.댓글 저장) 다음 버튼을 클릭해서 페이지 이동.
- 마지막 페이지에서는 다음 버튼이 disabled 되어 click()시 Exception발생
- Exception 발생하면 크롤링을 멈춘다.
import 및 크롬브라우저 실행
import time
from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 브라우저 띄우지 않고 하기
options = ChromeOptions()
options.add_argument('headless')
#driver load + url로 이동
driver = Chrome(options=options)
driver.get(url)
결과 댓글 크롤링 및 저장
comment_list = []
page = 0
while True:
page += 1
print(page)
more_btns = WebDriverWait(driver, 5).until(
EC.presence_of_all_elements_located([By.CSS_SELECTOR,'span.taLnk.ulBlueLinks']))
more_btns[0].click() #여러가리로 리스트이기 때문에 [0]으로 하나하나 불러와야 함
time.sleep(1)
comment_tags = driver.find_elements_by_css_selector('p.partial_entry')
# comment_tags = WebDriverWait(driver, 5).until(
# EC.presence_of_all_elements_located([By.CSS_SELECTOR,'p.partial_entry']))
for tag in comment_tags:
comment_list.append(tag.text)
#다음 버튼
next_btn = WebDriverWait(driver, 5).until(
EC.presence_of_element_located([By.CSS_SELECTOR,'a.nav.next']))
try:
next_btn.click()
except:
print('finish')
break
time.sleep(1)
#결과
![[Crawling] TripAdvisor 댓글 크롤링 4](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
728x90
반응형
'Data Engineering > Crawling' 카테고리의 다른 글
[Crawling] 네이버 영화 평점 및 댓글 크롤링 (3) | 2020.08.05 |
---|---|
[Crawling] 다나와(danawa) 제품 리스트 크롤링 (0) | 2020.08.05 |
[Crawling] Headless 브라우저 (0) | 2020.08.05 |
[Crawling] YouTube 동영상 목록 크롤링 (0) | 2020.08.05 |
[Crawling] Daum, Naver 로그인 후 메일 목록 크롤링 (0) | 2020.08.05 |