Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='path/to/chromedriver.exe')
driver.get('https://www.bing.com/videos/search?q=somebody+toucha+my+spaghet&FORM=HDRSC3&cc=us')
time.sleep(1)
# scrolls until "more videos" button is located
while True:
end_result = driver.find_element_by_xpath('//*[@id="bop_coll"]').is_displayed()
driver.find_element_by_xpath('//*[@id="b-scopeListItem-video"]/a').send_keys(Keys.END)
if end_result == True:
break
# wait for button to load, clicks on "more videos" button and continue code execution
time.sleep(2)
driver.find_element_by_css_selector('.mBtn').click()
# scrolls until the end of the video result with 60 retries buffer to break out of the while loop.
# this block of code works but could be better implemented.
# if you know a better solution, please, write in the comments below and I'll change this section to a better one.
# https://stackoverflow.com/a/34885906/15164646
tries = 60
for i in range(tries):
while True:
try:
driver.find_element_by_xpath('//*[@id="b-scopeListItem-video"]/a').send_keys(Keys.END)
except:
if i < tries - 1:
continue
else:
raise
break
# when 2 while loops executed it will iterate over every found item
# try/except used because sometimes there are no such elements being displayed
for index, result in enumerate(driver.find_elements_by_css_selector('.mc_vtvc.b_canvas')):
title = result.find_element_by_css_selector('.b_promtxt').text
link = f"https://www.bing.com{result.find_element_by_css_selector('.mc_vtvc_link').get_attribute('href')}"
try:
views = result.find_element_by_css_selector('.mc_vtvc_meta_row:nth-child(1) span:nth-child(1)').text
except:
views = None
try:
date = result.find_element_by_css_selector('.mc_vtvc_meta_row:nth-child(1) span+ span').text
except:
date = None
try:
video_platform = result.find_element_by_css_selector('.mc_vtvc_meta_row+ .mc_vtvc_meta_row span:nth-child(1)').text
except:
video_platform = None
try:
channel_name = result.find_element_by_css_selector('.mc_vtvc_meta_row_channel').text
except:
channel_name = None
print(f'{index}\n{title}\n{link}\n{channel_name}\n{video_platform}\n{date}\n{views}\n')
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment