How would one create a list containing the URL's of the videos from a normal YouTube text search?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With