Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resolving YouTube search links
#1
The goal of the program is as follows:
  1. perform a youtube search per user input
  2. print the first 5 search results to the terminal for user selection
  3. user makes selection
  4. 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: ")))    #
Reply


Messages In This Thread
Resolving YouTube search links - by pythonnewbie138 - Jul-28-2020, 04:20 PM
RE: Resolving YouTube search links - by Larz60+ - Jul-28-2020, 04:28 PM
RE: Resolving YouTube search links - by Larz60+ - Aug-01-2020, 02:09 AM
RE: Resolving YouTube search links - by j.crater - Aug-01-2020, 08:08 AM
RE: Resolving YouTube search links - by j.crater - Aug-01-2020, 07:16 PM
RE: Resolving YouTube search links - by Axel_Erfurt - Aug-01-2020, 08:13 PM
RE: Resolving YouTube search links - by snippsat - Aug-02-2020, 12:00 AM
RE: Resolving YouTube search links - by snippsat - Aug-02-2020, 10:27 PM
RE: Resolving YouTube search links - by snippsat - Aug-04-2020, 04:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  webscrapping links and then enter those links to scrape data kirito85 2 3,200 Jun-13-2019, 02:23 AM
Last Post: kirito85

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020