Apr-25-2021, 01:57 PM
Hello guys!
I encountered a situation which i think is good to learn.
I get 2 matches when im trying to find_all with Selenium.
Im trying to scrape views from Youtube.
Here is my code:
Thank you!
I encountered a situation which i think is good to learn.
I get 2 matches when im trying to find_all with Selenium.
Im trying to scrape views from Youtube.
Here is my code:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from bs4 import BeautifulSoup import time # Browser setup options = Options() options.add_argument("--headless") #options.add_argument("--window-size=1980,1020") browser = webdriver.Chrome(executable_path=r'/usr/bin/chromedriver', options=options) url = "https://www.youtube.com/channel/UCwTrHPEglCkDz54iSg9ss9Q/videos" browser.get(url) # Click Allow-button on Youtube consent-page consent = WebDriverWait(browser, 120).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="VfPpkd-RLmnJb"]'))) consent.click() # Send to BS soup = BeautifulSoup(browser.page_source, 'lxml') views = soup.find_all('span', {'class':'style-scope ytd-grid-video-renderer'}) for v in views: print("---------") print(v.text)There are 2 exactly the same blocks of code on Youtube:
<span class="style-scope ytd-grid-video-renderer">14 817 views</span> ...and... <span class="style-scope ytd-grid-video-renderer">7 hours ago</span>The output of my code:
--------- 42K views --------- 1 month ago --------- 119K views --------- 1 month ago ... ... ...I just want to get the views, is there a way to exclude the second output ("1 month ago"-output) ?
Thank you!