파이선 셀레니움 유튜브 댓글 수집

파이선 셀레니움 유튜브 댓글 수집

더시민 2 2007 0

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import csv
import pandas as pd

driver = webdriver.Chrome('C:/Users/@@@/AppData/Local/Programs/Python/Python39/chromedriver.exe')
driver.get('https://youtu.be/@@@@@@@@')
time.sleep(3)


# 셀레니움 옵션 설정
# options = webdriver.ChromeOptions()
# options.add_argument('headless') # 크롬 띄우는 창 없애기
# options.add_argument('window-size=1920x1080') # 크롬드라이버 창크기
# options.add_argument("disable-gpu") #그래픽 성능 낮춰서 크롤링 성능 쪼금 높이기
# options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36") # 네트워크 설정
# options.add_argument("lang=ko_KR") # 사이트 주언어
# driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
# 크롤링 목표 : 해당 영상에 대한 댓글 id, 댓글 내용, 댓글의 좋아요 개수, 날짜 추출
data_list = []
# 스크롤 내리기
body = driver.find_element_by_tag_name('body')
last_page_height = driver.execute_script("return document.documentElement.scrollHeight")
while True:
    driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
    time.sleep(3)
    new_page_height = driver.execute_script("return document.documentElement.scrollHeight")
    if new_page_height == last_page_height:
        break

try:
    driver.find_element_by_css_selector("#dismiss-button > a").click()
except:
    pass
buttons = driver.find_elements_by_css_selector("#more-replies > a")

time.sleep(1.5)

for button in buttons:
    driver.execute_script("arguments[0].click()", button)
    #button.send_keys(Keys.ENTER)
    time.sleep(2)
    #button.click()

   
# bs4 html 파싱
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")

id_final = []
comment_final = []

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('    ', '')
    id_final.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('    ', '')
    comment_final.append(temp_comment)
    
    

pd_data = {"아이디" : id_final , "댓글 내용" : comment_final}
youtube_pd = pd.DataFrame(pd_data)

youtube_pd.to_excel('youtube.xlsx')


driver.close()

2 Comments
더시민 2022.02.03 17:53  
def clean_text(inputString):
  text_sub = re.sub('[-=+,#/\?:^.@*\"※~ㆍ!』‘|\(\)\[\]`'…》\”\“\’·]', ' ', inputString)
  return text_sub

input = '개구리 어쩌구 - "엉덩이 흔들들 " [2020]'
string = clean_text(input)
print(string)
더시민 2022.02.03 16:37  
버튼 클릭은
buttons = driver.find_elements_by_css_selector("#more-replies > a")
이거보다는
buttons = driver.find_elements_by_css_selector("#more-replies")
로 해서
time.sleep(1.5)

for button in buttons:
    driver.execute_script("arguments[0].click()", button)
    #button.send_keys(Keys.ENTER)
    time.sleep(2)
    #button.click()
js실행으로 하는게 더 나은듯.
제목
Category
Facebook Twitter GooglePlus KakaoStory KakaoTalk NaverBand