๊ด๊ด์ํ ๋ฆฌ๋ทฐ ํฌ๋กค๋ง ๋ถ์
# only selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import pandas as pd
# selenium ์น ๋๋ผ์ด๋ฒ ์๋น์ค ์ค์ , ์คํ
service = Service(executable_path=ChromeDriverManager().install())
driver=webdriver.Chrome(service=service)
url='https://www.hanatour.com/trp/pkg/CHPC0PKG0200M200?pkgCd=AVP231241101ZEA&prePage=major-products&directSale=PM0000114930'
driver.get(url)
# ์ฌํํ๊ธฐ๋ฅผ ํด๋ฆญ click
# review_link=driver.find_element(By.XPATH,'//*[@id="sticky06-bottom"]')
# ์ข ๋ ์์ ํ๊ฒ ๊ธฐ๋ค๋ฆฐ ํ ํฌ๋กค๋ง
review_link=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="sticky06-bottom"]')))
review_link.click()
time.sleep(2)
# ๊ฐ ๋ฆฌ๋ทฐ ์ ๋ณด๋ฅผ ์ ์ฅํ ๋น ๋ฆฌ์คํธ
reviews=[]
driver.quit()
์นํ์ด์ง๊ฐ ๋ก๋ ๋๊ธฐ์ ์ ์ฌํํ๊ธฐ ๋ฒ๋ ์ ํด๋ฆญํ ์ ์๊ธฐ์
'์ฌํํ๊ธฐ' ๋ฒํผ์ด ๋ฐ๋๊น์ง ์์ ํ๊ฒ ๊ธฐ๋ค๋ฆฐ ํ ๋ฒํผ์ ๋๋ฌ ํฌ๋กค๋ง ํ๊ฒ ์ต๋๋ค.
F12๋ฅผ ๋๋ฅธ๋ค์ ๋ฆฌ๋ทฐ ๋งจ ์์ ์๋๊ฒ์ ๋๋ฅด๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋์ต๋๋ค.
li ๋ผ๋ ํ๊ทธ๊ฐ ๋ถ์ ๊ฒ๋ค์ด ์ฌ๋ฌ๊ฐ ์์ฃ ? ์ด๊ฑธ ๋ค ๊ฐ์ ธ์ฌ ๊ฒ๋๋ค.
์ด๋ฒ์ copy selector๋ฅผ ๋๋ฌ์ฃผ์ธ์
# li ํ๊ทธ๋ค์๊ฐ์ ธ์ค๋ ๋ถ๋ถ
lis=driver.find_elements(By.CSS_SELECTOR,'ul.list_review_v2>li') # ul class="list_review_v2" ์๋์ ์๋ ํ๊ทธ๋ค์ ๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ
ul class="list_review_v2" ์๋์ ์๋ ํ๊ทธ๋ค์ ๋ชจ๋ ๊ฐ์ ธ์์ต๋๋ค.
์ด์ ์ด ์ ๋ณด๋ค์ ๊ฐ์ ธ์ฌ ๊ฒ๋๋ค.
F12๋ฅผ ๋๋ฅด๋ฉด์ ์ด๋ป๊ฒ ๊ตฌ์ฑ๋์ด์๋์ง ์ง์ ํ์ธํด์ผ ํฉ๋๋ค.
๋ณ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ค๋ฉด strong ํ๊ทธ ๋ถ๋ถ์ ๊ฐ์ ธ์์ผ ํฉ๋๋ค.
์ข์ธก์ ์ปค์๋ฅผ ๋๊ณ ํ๋์ฉ ์ฎ๊ธฐ๊ฒ ๋๋ฉด strong ํ๊ทธ์ ์์น๊ฐ ๋ฐ๋๊ฒ ๋ฉ๋๋ค.
๋ค์ ํ๊ทธ์ ์ด๋ฆ์ A์ด๊ธด ํ์ง๋ง, ํ์ฌ ์ ๊ฐ ์์นํ ํ๊ทธ์์ ํ๋ ๋ ํฐ ํ ์คํธ๋ฅผ ์ฐพ์์ ์ ๋ ฅํด์ผ ํฉ๋๋ค.
3page์ XPath๋ฅผ ์นดํผํด์ต๋๋ค.
# selenium ์น ๋๋ผ์ด๋ฒ ์๋น์ค ์ค์ , ์คํ
service = Service(executable_path=ChromeDriverManager().install())
driver=webdriver.Chrome(service=service)
url='https://www.hanatour.com/trp/pkg/CHPC0PKG0200M200?pkgCd=AVP231241101ZEA&prePage=major-products&directSale=PM0000114930'
driver.get(url)
# ์ฌํํ๊ธฐ๋ฅผ ํด๋ฆญ click
# review_link=driver.find_element(By.XPATH,'//*[@id="sticky06-bottom"]')
# ์ข ๋ ์์ ํ๊ฒ ๊ธฐ๋ค๋ฆฐ ํ ํฌ๋กค๋ง
review_link=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="sticky06-bottom"]')))
review_link.click()
time.sleep(2)
# ๊ฐ ๋ฆฌ๋ทฐ ์ ๋ณด๋ฅผ ์ ์ฅํ ๋น ๋ฆฌ์คํธ
reviews=[]
for page_num in range(1,230) :# 1๋ถํฐ 299๊น์ง ๋ฐ๋ณต
# ํ์ด์ง๋ค์ด์
ํ ์์ ์ฐพ๊ธฐ
# paginate_div=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,'pagenate')))
# paginate_div=driver.find_element(By.CLASS_NAME,'pagenate')
# ์๋๋ class_name, 'pagenate'๋ก ์ฐพ์ผ๋ ค ํ์ผ๋ ์๋ ๋ฉํธ๋ฅผ ์ฐพ์์ ์๋ค๊ณ ๋์์ xpath๋ก ๋ณ๊ฒฝ
paginate_div=driver.find_element(By.XPATH,'//*[@id="sticky06"]/div/div[4]/div[3]/div')
# ํ์ฌ ๋ช ํ์ด์ง ํฌ๋กค๋ง ์ค์ธ์ง, ์ถ๋ ฅ
print("ํ์ฌ ํ์ด์ง:",page_num)
# li ํ๊ทธ๋ค์๊ฐ์ ธ์ค๋ ๋ถ๋ถ
lis=driver.find_elements(By.CSS_SELECTOR,'ul.list_review_v2>li') # ul class="list_review_v2" ์๋์ ์๋ ํ๊ทธ๋ค์ ๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ
for li in lis:
# ํ๋์ ๋ฆฌ๋ทฐ ์ ๋ณด๋ฅผ ์ ์ฅํ ๋์
๋๋ฆฌ
review_info={}
# rating_info์์ ๊ฐ ์ ๋ณด๋ฅผ ์ถ์ถ
rating_info=li.find_element(By.CLASS_NAME,'rating_info')
# ๋ณ์ : strong ํ๊ทธ ์์ผ๋ฉด ์ถ์ถ, ์์ผ๋ฉด ""ArithmeticErrorr
rating=rating_info.find_element(By.TAG_NAME, 'strong').text if rating_info.find_element(By.TAG_NAME,'strong') else ""
review_info['rating']=rating
#span ํ๊ทธ๋ค์ text๋ฅผ ์ฐจ๋ก์ฐจ๋ก๋ก review_info์ ๊ฐ ํญ๋ชฉ์ ์ ์ฅ
spans=rating_info.find_elements(By.TAG_NAME,'span')
review_info['user'] = spans[1].text
review_info['category']=spans[2].text
review_info['age']=spans[3].text
review_info['date']=spans[-1].text
# review_cont con ํด๋์ค ์์ ๋ฆฌ๋ทฐ ์ถ์ถ
review=li.find_element(By.CLASS_NAME,'review_cont')
review_text=review.text if review else ""
review_info['review']=review.text
# review_cate ํด๋์ค์์ ๋ฆฌ๋ทฐ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ์ถ์ถ
review_cate=li.find_element(By.CLASS_NAME,'review_cate')
review_cate_text=review_cate.text if review_cate else ""
review_info['review_cate']=review_cate_text
reviews.append(review_info)
# ๋ค์ ํ์ด์ง๋ก ์ด๋
try:
# ํ์ด์ง๊ฐ 1~9, 11~19, 21~29
if page_num % 10 != 0:
next_link=paginate_div.find_element(By.XPATH,f"//span/a[text()='{page_num+1}']")
# a ํ๊ทธ ์์ text์ ๋ชจ์์ด, page_num์ 1์ ๋ํ ๊ฐ์ด aํ๊ทธ์ text ๊ฐ์ผ๋ก ๋ค์ด์์ผ๋ฉด ๊ทธ๊ฑธ ๊ฐ์ ธ์ค๊ธฐ
# ๋ค์ ํ๊ทธ์ ์ด๋ฆ์ A์ด๊ธด ํ์ง๋ง, ํ์ฌ ๋ด๊ฐ ์์นํ ํ๊ทธ์์ ํ๋ ๋ ํฐ ํ
์คํธ๋ฅผ ์ฐพ์์ ์
๋ ฅํด์ผ ํ๊ธฐ์
next_link.click()
# ํ์ด์ง๊ฐ 10, 20, 30
else:
next_link=paginate_div.find_element(By.XPATH,'//*[@id="sticky06"]/div/div[4]/div[3]/div/div/a[3]')
next_link.click()
time.sleep(2) # ๋ค์ ๋ฒํผ ๋๋ฅผ๋๊น์ง ์ ๊น ๊ธฐ๋ค๋ฆฌ๊ธฐ
except:
print("๋ง์ง๋ง ํ์ด์ง์ ๋๋ฌํ์ต๋๋ค.")
break
#์ข
๋ฃ
driver.quit()
# ๋ฐ์ดํฐ ํ๋ ์ ์์ฑ
df=pd.DataFrame(reviews)
1~229ํ์ด์ง์ ๊ด๊ด์ํ์ ๊ฐ์ ธ์ฌ ์ ์์์ต๋๋ค.
์ด์ ์์งํ ์๋ฃ๋ฅผ ๊ฐ์ง๊ณ ์๊ฐํ๋ฅผ ํตํ ๋ฐ์ดํฐ ๋ถ์์ ํ๊ฒ ์ต๋๋ค.
'๐ฆ๋ถ์ ํ๋ก์ ํธ > ๐ ํฌ๋กค๋ง ๋ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ ํฌ๋กค๋ง ๋ถ์ ํ๋ก์ ํธ (์) (6) | 2024.10.19 |
---|---|
๐ ํฌ๋กค๋ง ๋ถ์ ํ๋ก์ ํธ (7) - ์๊ฐํ (1) | 2024.10.19 |
๐ ํฌ๋กค๋ง ๋ถ์ ํ๋ก์ ํธ (4) - ์ฝ๋ ์ผ ๊ธฐ์ฐจํ ํฌ๋กค๋ง (0) | 2024.10.17 |
๐ ํฌ๋กค๋ง ๋ถ์ ํ๋ก์ ํธ (3) - RSS ํ์ฉ ๋ด์ค ํฌ๋กค๋ง (0) | 2024.10.17 |
๐ ํฌ๋กค๋ง ๋ถ์ ํ๋ก์ ํธ (2) - Selenium ํ์ฉ ๋ด์ค ํฌ๋กค๋ง (1) | 2024.10.17 |