Jul-28-2020, 04:20 PM
The goal of the program is as follows:
Libraries being utilized:
I went through a number of example scripts trying to understand how the process works and then trying to get some sort of successful result. This is as close as I've gotten. I hit a wall with having no search results printed to the terminal for selection. I'll paste my code below. I've commented as much as I understand well but would appreciate some help with explaining the lines that don't have comments at the end (or are incorrect) and help understanding why no search results are being returned from the query.
I know little about html and think that's part of my struggle in writing this one.
- perform a youtube search per user input
- print the first 5 search results to the terminal for user selection
- user makes selection
- print the chosen URL to the terminal
Libraries being utilized:
- requests
- urllib
- BeautifulSoup
I went through a number of example scripts trying to understand how the process works and then trying to get some sort of successful result. This is as close as I've gotten. I hit a wall with having no search results printed to the terminal for selection. I'll paste my code below. I've commented as much as I understand well but would appreciate some help with explaining the lines that don't have comments at the end (or are incorrect) and help understanding why no search results are being returned from the query.
I know little about html and think that's part of my struggle in writing this one.
# import necessary modules import requests import urllib from bs4 import BeautifulSoup # definte youtube search function def getYoutube(name, year): # 2 arguments - movie name and year choice = "" # start choice as empty textToSearch = '{0} {1} + "trailer"'.format(name, year) query = urllib.parse.quote(textToSearch) # create search string url = "https://www.youtube.com/results?search_query=" + query # create search URL object response = urllib.request.urlopen(url) html = response.read() soup = BeautifulSoup(html, "html.parser") # bs parses html inc = 1 # incremental counter to break loop for vid in soup.findAll(attrs={'class': 'yt-uix-tile-link'}): # identify trailer links and print to terminal print("#{0}: {1}".format(inc, vid['title'])) # format text to print nicely and print print(" ") inc += 1 # counter increase by 1 for each loop if inc >5: # break out after 5 loops break while True: # loop to choose trailer choice = input("Pick a trailer: ") # input trailer choice per printed items if choice.isdigit(): # if user entered a number, choice = int(choice) # convert input to integer break # break out of loop else: return 0 print("You chose: " + soup.findAll(attrs={'class': 'yt-uix-tile-link'})[choice - 1]['title']+"\n") # print user choice yt_url_end = soup.findAll(attrs={'class': 'yt-uix-tile-link'})[choice - 1]['href'] # create video string object to concat with baseurl return "http://www.youtube.com" + yt_url_end #return chosen URL print(yt_url_end) getYoutube(input(str("Enter a Movie: ")), input(str("Enter a Year: "))) #