Reputation: 67
I don't know why this code does not work. When I print the list "videos" and "search results" to see what is happening, they are both empty. This is why the if statement is never reached as you can see in the screenshot.
# Query youtube results based on the songs entered in the text file
def find_video_urls(self, songs):
videos = list()
for song in songs:
self.update_text_widget("\nSong - " + song + " - Querying youtube results ...")
query_string = urllib.parse.urlencode({"search_query": song})
with urllib.request.urlopen("http://www.youtube.com/results?" + query_string) as html_content:
# retrieve all videos that met the song name criteria
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
# only take the top result
if len(search_results) != 0:
videos.append("https://www.youtube.com/watch?v=" + search_results[0])
self.update_text_widget("\nSong - " + song + " - Found top result!")
return videos
Upvotes: 0
Views: 346
Reputation: 897
Hi Vigilante I will share a code on how to do it with requests. Maybe you can implement it in your code.
import re
import requests
def find_video_urls(songs):
videos = list()
for song in songs:
with requests.session() as ses:
r = ses.get('http://www.youtube.com/results', params={"search_query": song})
search_results = re.findall(b'(/watch\?v=.{11})\"', r.content, re.MULTILINE | re.IGNORECASE | re.DOTALL)
print(search_results)
# only take the top result
if len(search_results) != 0:
videos.append(b"".join([b'https://www.youtube.com', search_results[0]]))
return videos
print(find_video_urls(['Eminem - Lose Yourself']))
Upvotes: 1