Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning the URL's as a list from a YouTube search query [closed]

How would one create a list containing the URL's of the videos from a normal YouTube text search?

like image 689
Petri Avatar asked Mar 16 '15 04:03

Petri


People also ask

How do I find the embedded ID on YouTube?

How to get a YouTube video ID from a youtube.com page URL. You may be watching the video or just happened to visit a link to a video. The video ID will be located in the URL of the video page, right after the v= URL parameter.


1 Answers

I wrote you a quick script to do this. Replace textToSearch with your query. It 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. Let me know if you have any questions.

import urllib.request
from bs4 import BeautifulSoup

textToSearch = 'hello world'
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'}):
    print('https://www.youtube.com' + vid['href'])

this gives the output:

https://www.youtube.com/watch?v=al2DFQEZl4M
https://www.youtube.com/watch?v=pRKqlw0DaDI
...
https://www.youtube.com/watch?v=9sQEQkMDBjw
like image 198
Jason Brooks Avatar answered Oct 13 '22 01:10

Jason Brooks