top of page

Insert Youtube Information Into A Database -YoutubeCiteDb.py | Python 3 Source Code

  • Writer: Brian Clark
    Brian Clark
  • Sep 9, 2022
  • 2 min read

Thank you for visiting the web page. Here is an example of a python program that takes the HTML elements of a website and places them into a database file. ;



from calendar import c
import pprint as pp
import re
from bs4 import BeautifulSoup
import requests
import sqlite3 
import os.path


'''
Author: Brian Clark 
Website: https://brianclark88.wixsite.com/
Description: The program scrapes youtube for APA citation 
Date: 7/31/2022 
References: Pythoncode https://www.thepythoncode.com/article/get-youtube-data-python
Crummy (2022) Beautiful Soup Documentation.Crummy. https://www.crummy.com/software/BeautifulSoup/bs4/doc/
Donate only if you can: Cashapp: $TcfTradingGroupLLC  | Venmo: @Brian_Clark-386 | Paypal: https://paypal.me/tcftrading?country.x=US&locale.x=en_US
Job listing for entry level developer: Front end tech Javascript and html5 Knowledge of object relational mapping(ORM) django
NOTE: Don't forget to use a print() statment to see where your code is not working 

'''
def run_Db():
     '''' __________________________________
          ________ Create Database _________
     '''
     
     
     #setup a connection to the database 
     conn = sqlite3.connect('citation.db')

     #Cursor allows us to execute commands to the db
     cur = conn.cursor() 
     #execute our query
     cur.execute('''CREATE TABLE IF NOT EXISTS elements
                    (Channel text PRIMARY KEY, Date text, Title text, Website text, Url real  )''')

     page = requests.get(input("__\n\n Enter Youtube Url: \n\n__"))
     soup = BeautifulSoup(page.content, "html.parser")

     # Print the channel name of the youtube channel
     # Variables
     # ______________________________________________


     channel_name = soup.find("span", itemprop="author").next.next['content']
     result = soup.find("meta", itemprop="datePublished")['content']
     

     channel_title = soup.title.string
     channel_url = soup.find("span", itemprop="author").next['href']
     channel_title_two = soup.find("meta", itemprop="name")["content"]
     website = "YouTube"

     channel_info =[]
     channel_info.append((channel_name, result,
                    channel_title_two,website,channel_url))

     #____________________________________________________________


     cur.executemany("INSERT OR IGNORE INTO elements VALUES (?,?,?,?,?)", channel_info)
     conn.commit()
     cur.close()

run_Db()

#We now need to parse the HTML and load it into a BS4 structure.




'''
channel_name = (id="channel-name")
channel_class = (class="yt-simple-endpoint style-scope yt-formatted-string")

'''


#End_program = input("Press any button to end the program")


# Date published
#   print(result)


# Title of a web page printed
#    channel_title = soup.title.string


# To create an apa citation I need the username of the youtube page,


# the month and year of the video

# all textfrom a webpage
#print("\n.............")
#print(soup.get_text())


# extracting URLS
'''
print("\n.....................")
for link in soup.find_all('a'):
    print(link.get('href'))
'''
# Extract view count
#  print("\n","Views:", soup.find("meta", itemprop="interactionCount")['content'],".......\n\n")




If you want to view the "citation.DB" file, you can use the following link: DB Browser for SQLite. DB Browser is a Beneficial program for viewing your database files.


Simply cut the youtube URL and right-click to paste it into the prompt, and you will have your YouTube citation placed inside the citation.db file.

Comments


bottom of page