Created
January 16, 2022 07:36
-
-
Save harendra21/a545919308d79fd964d20c54163de8cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
import csv | |
import time | |
items=[] | |
driver=webdriver.Chrome(r"C:/Users/hp/Anaconda3/chromedriver.exe") | |
driver.get('https://www.youtube.com/watch?v=iFPMz36std4') | |
driver.execute_script('window.scrollTo(1, 500);') | |
#now wait let load the comments | |
time.sleep(5) | |
driver.execute_script('window.scrollTo(1, 3000);') | |
username_elems = driver.find_elements_by_xpath('//*[@id="author-text"]') | |
comment_elems = driver.find_elements_by_xpath('//*[@id="content-text"]') | |
for username, comment in zip(username_elems, comment_elems): | |
item = {} | |
item['Author'] = username.text | |
item['Comment'] = comment.text | |
items.append(item) | |
filename = 'C:/Users/hp/Desktop/commentlist.csv' | |
with open(filename, 'w', newline='', encoding='utf-8') as f: | |
w = csv.DictWriter(f,['Author','Comment']) | |
w.writeheader() | |
for item in items: | |
w.writerow(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment