当前,我使用Selenium Web驱动程序刮取YouTube视频评论。它成功刮取评论,但输出显示在控制台上。请帮助我解决此问题。
from selenium import webdriver
import csv
import time
driver=webdriver.Chrome("C:\\Users\\Shahnawaz Irfan\\Desktop\\TrendMania\\FYP-2\\Scrapping code\\chromedriver_win32\\chromedriver.exe")
driver.get('https://www.youtube.com/watch?v=TYMzLJJqhkU&pbjreload=10')
driver.execute_script('window.scrollTo(1, 500);')
time.sleep(5)
driver.execute_script('window.scrollTo(1, 3000);')
comment_div=driver.find_element_by_xpath('//*[@id="contents"]')
comments=comment_div.find_elements_by_xpath('//*[@id="content-text"]')
for comment in comments:
csvData = [print(comment)]
with open('demo.csv', 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(csvData)
csvFile.close()