the code is able to get the commenters usernames and is able to click on the replies button to show the people that replie to a comment but i just can’t get it to extract the usernames of the people that replied to a comment . i don’t know why but i tried to extract the usernames of the people that replied but they just don’t show up on the result
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def extract_usernames(video_url):
# Initialize the Chrome driver
driver = webdriver.Chrome()
# Navigate to the video URL
driver.get(video_url)
# Wait for the page to load completely
time.sleep(10) # Increase this value if your internet connection is slow
# Scroll to the bottom of the page to load all comments
last_height = driver.execute_script("return document.documentElement.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
time.sleep(2) # Slowly scroll down to load all comments
new_height = driver.execute_script("return document.documentElement.scrollHeight")
if new_height == last_height:
break
last_height = new_height
# Find all the comment elements
comment_elements = driver.find_elements(By.CSS_SELECTOR, "ytd-comment-thread-renderer")
# Extract the usernames from the comment elements (including replies)
usernames = []
for comment_element in comment_elements:
# Click on the "View replies" button if it exists
try:
view_replies_button = comment_element.find_element(By.CSS_SELECTOR, "#more-replies.more-button.style-scope.ytd-comment-replies-renderer")
view_replies_button.click()
time.sleep(2) # Wait for replies to load after clicking
except:
pass
extract_usernames_recursive(comment_element, usernames)
# Close the browser
driver.quit()
return usernames
def extract_usernames_recursive(comment_element, usernames):
# Extract the username from the top-level comment
username_element = comment_element.find_element(By.CSS_SELECTOR, "h3.style-scope.ytd-comment-view-model > a.yt-simple-endpoint.style-scope.ytd-comment-view-model")
username_link = username_element.get_attribute('href')
if username_link.startswith('https://www.youtube.com/@'):
username = username_link.split('@')[1]
usernames.append(username)
# Check for replies and extract usernames recursively
reply_elements = comment_element.find_elements(By.CSS_SELECTOR, "ytd-comment-thread-renderer")
for reply_element in reply_elements:
extract_usernames_recursive(reply_element, usernames)
# Example usage
video_url = "https://www.youtube.com/watch?v=A1III_DQU4I" # Replace with the actual video URL
usernames = extract_usernames(video_url)
print(usernames)