Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dimitryzub
Created July 17, 2021 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitryzub/b83db5c1a7f4ff94ac4b9ee9931bd790 to your computer and use it in GitHub Desktop.
Save dimitryzub/b83db5c1a7f4ff94ac4b9ee9931bd790 to your computer and use it in GitHub Desktop.
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