상세 컨텐츠

본문 제목

유튜브 댓글 크롤링

카테고리 없음

by 천승원 2023. 3. 29. 21:56

본문

필요할때마다 찾으면 없는 코드 정리하기

다른 블로그 보면 좀 멋있게 코드처럼 쓰던데 

몇몇 코드를 주워다가 에러뜨는부분을 수정해서 올립니다.

2023/3/29 잘돌아감

 

from selenium import webdriver                                  #크롬드라이버 가져오는 아이
from selenium.webdriver.common.keys import Keys         

from selenium.webdriver.common.by import By        
from bs4 import BeautifulSoup                                   
import time                                                      #sleep 안쓰면 차단당함
from openpyxl import Workbook
import pandas as pd
import warnings 
warnings.filterwarnings('ignore')

 

 

wb = Workbook(write_only=True)
ws = wb.create_sheet()

driver = webdriver.Chrome("chromedriver.exe")   #버전에 맞게 설치
driver.get("url 입력")
driver.implicitly_wait(3)

time.sleep(1.5)

driver.execute_script("window.scrollTo(0, 800)")
time.sleep(3)

 

 

 

 

last_height = driver.execute_script("return document.documentElement.scrollHeight")

while True:
    driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
    time.sleep(1.5)

    new_height = driver.execute_script("return document.documentElement.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

time.sleep(1.5)

 

 

buttons = driver.find_element(By.CSS_SELECTOR,"#more-replies > a")

time.sleep(1.5)

for button in buttons:
    button.send_keys(Keys.ENTER)
    time.sleep(1.5)
    button.click()

 

 

html_source = driver.page_source
soup = BeautifulSoup(html_source, 'html.parser')

id_list = soup.select("div#header-author > h3 > #author-text > span")
comment_list = soup.select("yt-formatted-string#content-text")

user_id = []
comments = []

for i in range(len(comment_list)):
    temp_id = id_list[i].text
    temp_id = temp_id.replace('\n', '')
    temp_id = temp_id.replace('\t', '')
    temp_id = temp_id.replace('    ', '')
    user_id .append(temp_id) # 댓글 작성자

    temp_comment = comment_list[i].text
    temp_comment = temp_comment.replace('\n', '')
    temp_comment = temp_comment.replace('\t', '')
    temp_comment = temp_comment.replace('    ', '')
    comments.append(temp_comment) # 댓글 내용

 

 

data_dict = {"아이디" : user_id , "댓글 내용" : comments }
youtube_pd = pd.DataFrame(data_dict)

youtube_pd.to_excel('result.xlsx')

 

 

 

댓글 영역