How can I scrape videos from a YouTube search?

Viewed 5299

I want to search for a specific keyword and then scrape all the videos URLs.

I know the code I am about to paste is not going to do that, but I want to show what I have done.

chrome_path = r"C:\Users\Admin\Documents\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.youtube.com/results?sp=CAISAggBUBQ%253D&q=minecraft")

links = driver.find_elements_by_partial_link_text('/watch')
for link in links:
    links = (links.get_attribute("href"))

How can I scrape the links and save them to a file?

3 Answers

This script pulls the results from the first page of YouTube results using urllib, and prints all the links of the videos by parsing the page using BeautifulSoup(if you are using python 3.* then install BeautifulSoup4).

import urllib.request
from bs4 import BeautifulSoup

textToSearch = 'python tutorials'
query = urllib.parse.quote(textToSearch)
url = "https://www.youtube.com/results?search_query=" + query
response = urllib.request.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
    if not vid['href'].startswith("https://googleads.g.doubleclick.net/"):
        print('https://www.youtube.com' + vid['href'])
Related