Skip to content Skip to sidebar Skip to footer

Scrape Youtube Video Url From A Specific Channel To Mysql

I would like to use this code and save the url that I obtain in a field of a mysql database. from bs4 import BeautifulSoup from lxml import etree import urllib import requests impo

Solution 1:

ok, first you should install mysql.connector, actually the hardest job is here! pip install mysql.connector more info at: How to install MySQL connector

after installing add the module then define the insert_to_mysql function like this:

import mysql.connector # add this modulefrom bs4 import BeautifulSoup
from lxml import etree
import urllib
import requests
import sys

# mysql connection setup
mydb = mysql.connector.connect(
  host="127.0.0.1",
  user="root",
  password="my@password",
  database="mydb1"
)


definsert_to_mysql(title, url): # define this function
  mycursor = mydb.cursor()
  sql = "INSERT INTO tbl1 (name, url) VALUES (%s, %s)"
  val = (title, url)
  mycursor.execute(sql, val)
  mydb.commit()
  print(mycursor.rowcount, "record inserted.")


deffetch_titles(url):
    video_titles = []
    html = requests.get(url)
    soup = BeautifulSoup(html.text, "lxml")
    for entry in soup.find_all("entry"):
        for link in entry.find_all("link"):
            youtube = etree.HTML(urllib.request.urlopen(link["href"]).read()) 
            video_title = youtube.xpath("//span[@id='eow-title']/@title") 
            iflen(video_title)>0:
                video_titles.append({"title":video_title[0], "url":link.attrs["href"]})
    return video_titles

defmain():
    if sys.argv.__len__() == 1:
        print("Error: You should specifying keyword")
        print("eg: python3 ./main.py KEYWORD")
        return

    url="https://www.youtube.com/feeds/videos.xml?user=LinusTechTips"
    keyword = sys.argv[1]

    video_titles = fetch_titles(url)
    for video in video_titles:
        if video["title"].__contains__(keyword):
            print(video["url"])
            insert_to_mysql(video["title"], video["url"])  # call the function herebreakif __name__ == "__main__":
    main()

Read this w3 article about pyhon and MySQL - good place for starters: python_mysql_getstarted.asp

Post a Comment for "Scrape Youtube Video Url From A Specific Channel To Mysql"