1

I am trying to create a function that takes a search term from a list and add it to a list of links.

formatted_link = link_to_vid.replace(' ', '+')
driver.get('https://www.youtube.com/results?search_query={}'.format(str(formatted_link)))

Then extracts the first link that YouTube returns. For example, I search 'Never Gonna Give You Up' and it adds the link of the first result to a list.

Then I may want to do ['Never Gonna Give You Up', 'Shrek']

How would I do this without actually clicking on the link?

Samay Lakhani
  • 85
  • 1
  • 1
  • 7

1 Answers1

1

I hope, I got your question right, here is an example:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.implicitly_wait(5)

# Let's create a list of terms to search for and an empty list for links
search_terms = ['Never Gonna Give You Up', 'Shrek']
links = []

for term in search_terms:
    # Open YouTube page for each search term
    driver.get('https://www.youtube.com/results?search_query={}'.format(term))
    # Find a first webelement with video thumbnail on the page
    link_webelement = driver.find_element(By.CSS_SELECTOR, 'div#contents ytd-item-section-renderer>div#contents a#thumbnail')
    # Grab webelement's href, this is your link, and store in a list of links:
    links += [link_webelement.get_attribute('href')]

print(links)

Hope this helps. Keep in mind, that for scraping data like this, you don't have to open webpages and use selenium, there are libraries like Beautiful Soup that help you do this kind of things faster.

Svetlana Levinsohn
  • 1,550
  • 3
  • 10
  • 19