Flask/ゆーちゅーばーFile(Python.Flask版)

app.py
import os
from flask import Flask, request, render_template

import requests
from bs4 import BeautifulSoup

import random

import MySQLdb

app = Flask(__name__)

@app.route('/')
def index():
    db_config = {
        'host': 'localhost',
        'db': 'test',  # Database Name
        'user': 'root',
        'passwd': '',
        'charset': 'utf8',
    }
 
    try:
    # 接続
        conn = MySQLdb.connect(host=db_config['host'], db=db_config['db'], user=db_config['user'],passwd=db_config['passwd'], charset=db_config['charset'])
    except MySQLdb.Error as ex:
        print('MySQL Error: ', ex)

    cursor = conn.cursor()
    
    lists = []
    # 結果セットを辞書型で取得
    dict_cursor = conn.cursor(MySQLdb.cursors.DictCursor)
    
    dict_cursor.execute('SELECT * FROM youtubers')
    
    for row in dict_cursor:
        lists.append(row)
    
    random.shuffle(lists)
    
    class Youtubers:
        def __init__(self, url, genre):
            self.url = url
            self.genre = genre
            response = requests.get(url)
            soup = BeautifulSoup(response.text,'lxml')
            self.name = soup.find('h1').get_text()
            self.registrants = soup.find(attrs={"class", "yt-subscription-button-subscriber-count-branded-horizontal"}).get_text()
            about = requests.get(url + "/about")
            soup02 = BeautifulSoup(about.text,'lxml')
            self.totalViews = soup02.select_one('span[class="about-stat"] > b').get_text()
            videos = requests.get(url + '/videos')
            soup03 = BeautifulSoup(videos.text,'lxml')
            self.days = soup03.select('[class="yt-lockup-meta-info"] > li')[1].get_text()
            self.title = soup03.findAll("h3")[3].get_text()
            img = soup.findAll('img')[0]
            self.src = img.get('src')
    
    #print(dict_cursor.rowcount) データ数
    
    You00 = Youtubers(lists[0]["url"], lists[0]["genre"])
    You01 = Youtubers(lists[1]["url"], lists[1]["genre"])
    You02 = Youtubers(lists[2]["url"], lists[2]["genre"])
    
    youlists = [You00, You01, You02]

    cursor.close
    conn.close
    
    return render_template('index.html', youlists=youlists)

@app.route("/contact")
def contact():
    return render_template("contact.html")

if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True)