# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # WELCOME TO THE YOUTUBE SCRAPER USING BS4 

# Hello All, I will show you all how to scrape youtube and display the video in the Notebook itself. Also, we can store the details in the excel for further use.
# Before Scraping any website, go through their terms and conditional.
#
# You can find the code in github - https://github.com/JohnPravin97/Youtube_Scraper

# ## To Import the Needed Library
#

from bs4 import BeautifulSoup 
from urllib.request import urlopen, Request
from selenium import webdriver
import pandas as pd
from IPython.display import IFrame, YouTubeVideo
#from webdriver_manager.chrome import ChromeDriverManager
import os


# ## Scraper Function

def youtube_scraper(inp):
    
    Search = '+'.join(inp.split())
    
    driver = webdriver.Chrome(executable_path=r'C:\Users\jpravijo\Desktop\Anaconda\chromedriver_win32 (2)\chromedriver.exe')
    driver.get('https://www.youtube.com/results?search_query='+Search)
    html = driver.page_source
    soup = BeautifulSoup(html)
    search = soup.find('body', dir='ltr')
    first_content = soup.find('div', id='content')
        
    link,name,channel=[],[],[]
    
    for i,second_content in enumerate (first_content.find_all('div', class_='text-wrapper style-scope ytd-video-renderer')):
        try:

            third_content=second_content.find('h3', class_='title-and-badge style-scope ytd-video-renderer')

            # To get the link of the song
            link.append(('https://www.youtube.com'+(third_content.a)['href']).strip())


            # To get the name of the song
            k=third_content.a.text.strip()
            name.append(k)

            # To get the channel details of the songs
            channel.append(second_content.find('div', class_='hidden style-scope paper-tooltip').text.strip())
            
            if i>10:
                    break

        except:
            pass
    dic={'Name of the Songs': name, 'Channel': channel, 'Links':link}   
    print ('\n')
    print ('\033[1m' + 'These are the top 5 searches from youtube for your search'.center(50))
    driver.close()
    return dic

inp=input('enter the song to play from youtube - Scraping the data: ')
df= pd.DataFrame(youtube_scraper(inp))
df.head()

# ## Choose the Scraped data to play the songs

print('\033[1m'+"please select the songs by choosing the indexes (e.g) 0,1,2,3,4")
song_input=int(input())
YouTubeVideo(df.loc[song_input, 'Links'].split('=')[1], width=800, height=600)

# ## To save the dataframe into HDF Format and CSV format

hdf=pd.HDFStore(r'C:\Users\jpravijo\Desktop\Anaconda\youtube.h5');
hdf.put(inp,df,format='table', data_columns=True);
df.to_csv(r'C:\Users\jpravijo\Desktop\Anaconda\youtube.csv',mode='a');

# ## To open the CSV which contains the Dataframe

os.startfile(r'C:\Users\jpravijo\Desktop\Anaconda\youtube.csv')

# + [markdown] heading_collapsed=true
# ## To open the HDF which contains the Dataframe

# + hidden=true
hdf=pd.HDFStore(r'C:\Users\jpravijo\Desktop\Anaconda\youtube.h5')
hdf.keys()

# + hidden=true
hdf.get('/Alan Walker Faded song')

# + [markdown] hidden=true
# THANK YOU 

StarcoderdataJupyterScriptsDedupFiltered