dimanche 1 août 2021

Trying to scrap data from youtube using selenium everything is working fine but the next video play button is not working correctly in the loop


#open the webpage
driver.get("https://www.youtube.com/c/Cashkaro-India/videos")


play_all = driver.find_element_by_xpath("//*[@class='style-scope ytd-button-renderer style-text size-default']")
play_all.click()


Reviews =pd.DataFrame() 

k=1
next= driver.find_element_by_css_selector('#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > a.ytp-next-button.ytp-button')
for i in range(10):
    
    for l in range(k):
        
        for j in range(k):
            
            all_posts = driver.find_elements_by_xpath('//*[@id="container"]/h1/yt-formatted-string')
            all_posts = [x.text for x in all_posts]
            posts = pd.Series(all_posts)
            
            all_views = driver.find_elements_by_xpath("//*[@class='short-view-count style-scope ytd-video-view-count-renderer']")
            all_views = [x.text for x in all_views]
            views = pd.Series(all_views)
            
            all_likes = driver.find_elements_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[8]/div[2]/ytd-video-primary-info-renderer/div/div/div[3]/div/ytd-menu-renderer/div/ytd-toggle-button-renderer[1]/a/yt-formatted-string')
            all_likes = [x.text for x in all_likes]
            likes = pd.Series(all_likes)
            
            all_dislikes = driver.find_elements_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[8]/div[2]/ytd-video-primary-info-renderer/div/div/div[3]/div/ytd-menu-renderer/div/ytd-toggle-button-renderer[2]/a/yt-formatted-string')
            all_dislikes = [x.text for x in all_dislikes]
            dislikes = pd.Series(all_dislikes)
            
            all_comments = driver.find_elements_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/ytd-comments/ytd-item-section-renderer/div[1]/ytd-comments-header-renderer/div[1]/h2/yt-formatted-string/span[1]')
            all_comments = [x.text for x in all_comments]
            comments = pd.Series(all_comments)
            
            df = pd.DataFrame({"Title":posts, "Views":views, "Likes":likes, "Dislikes":dislikes, "No of Comments":comments})
            
        Reviews = Reviews.append(df) 
        time.sleep(3)
    
    
    
    next.click()
    time.sleep(2)
    
Reviews

The loop is not working every time loop start the youtube next button doesn't work in this code the next play button is denoted by keyword (next) and i am trying the next.click() everything is working fine but the next click is not working as per the loop should goo and the next.click() shows error




Aucun commentaire:

Enregistrer un commentaire