Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

How to return the link to the first Youtube video after a search in Selenium Python?

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...