豆瓣电影Top250数据分析

一、requirements

beautifulsoup4==4.9.1
bs4==0.0.1
click==7.1.2
cycler==0.10.0
Flask==1.1.2
itsdangerous==1.1.0
jieba==0.42.1
Jinja2==2.11.2
kiwisolver==1.2.0
MarkupSafe==1.1.1
matplotlib==3.2.1
numpy==1.18.4
Pillow==7.1.2
pyparsing==2.4.7
python-dateutil==2.8.1
six==1.15.0
soupsieve==2.0.1
Werkzeug==1.0.1
wordcloud @ file:python_reptile/flask/static/extend/wordcloud-1.7.0-cp36-cp36m-win32.whl
xlwt==1.3.0

二、获取并存储数据

爬取豆瓣TOP250数据,并存储到数据库

步骤:

  1. 定义爬取地址

  2. 获取URL的数据列表

    通过User-Agent,得到指定一个URL的网页内容

  3. 存储到sqlite数据库(数据库名:movie.db,表名:movie250

# -*- coding:utf-8 -*-
 
# date: 2020-5-10
# author: jingluo
import sys
from bs4 import BeautifulSoup
import sqlite3
import re
import urllib.request, urllib.error
import xlwt

# 搜索规则
findLink = re.compile(r'<a href="(.*?)">')
findImageSrc = re.compile(r'<img.*src="(.*?)"', re.S) # re.S让换行符包含在字符中
findTitle = re.compile(r'<span class="title">(.*)</span>')
findRating = re.compile(r'<span class="rating_num" property="v:average">(.*)</span>')
findJudge = re.compile(r'<span>(\d*)人评价</span>')
findInq = re.compile(r'<span class="inq">(.*)</span>')
findBd = re.compile(r'<p class="">(.*?)</p>', re.S)

def main():
    # 1. 定义爬取网址
    base_url = "https://movie.douban.com/top250?start="
    # 2. 获取数据列表
    data_list = getData(base_url)
    # 3. 定义数据库名称
    dbpath = "movie.db"
    # 4. 存储到sqlite数据库
    saveData2DB(data_list, dbpath)

# 获取数据列表
def getData(base_url):
    data_list = []
    for i in range(0, 10):
        url = base_url + str(i*25)
        html = askURl(url)
        
        # 逐一解析网页
        soup = BeautifulSoup(html, "html.parser")
        for item in soup.find_all("div", class_="item"):
            data = []
            item = str(item)

            link = re.findall(findLink, item)[0]
            data.append(link)
            imgSrc = re.findall(findImageSrc, item)[0]
            data.append(imgSrc)
            titles = re.findall(findTitle, item)
            if len(titles) == 2:
                ctitle = titles[0]
                data.append(ctitle)
                otitle = titles[1].replace("/", "")
                data.append(otitle)
            else:
                data.append(titles[0])
                data.append('')
            rating = re.findall(findRating, item)[0]
            data.append(rating)
            judege = re.findall(findJudge, item)[0]
            data.append(judege)
            inq = re.findall(findInq, item)
            if len(inq) != 0:
                data.append(inq[0].replace("。", ""))
            else:
                data.append('')
            bd = re.findall(findBd, item)[0]
            bd = re.sub('<br(\s+)?/>(\s+)?', " ", bd)
            bd = re.sub('/', " ", bd)
            data.append(bd.strip())
            data_list.append(data)
    return data_list

# 得到指定一个URL的网页内容
def askURl(url):
    # 用户验证信息
    head = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"}
    request = urllib.request.Request(url, headers = head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print("请求出错",e.code)
        if hasattr(e, "reason"):
            print("错误原因",e.reason)
    return html

# 保存到sqlite数据库中
def saveData2DB(data_list, dbpath):
    init_db(dbpath)
    conn = sqlite3.connect(dbpath)
    cur = conn.cursor()

    for data in data_list:
        for index in range(len(data)):
            if index == 4 or index == 5:
                continue
            data[index] = '"' +data[index] + '"'
        sql = '''
        insert into movie250
        (
        info_link,pic_link,cname,ename,score,rated,instroduction,info
        )
        values(%s)'''%",".join(data)
        cur.execute(sql)
        conn.commit()
    cur.close()
    conn.close()

# 初始化数据库
def init_db(dbpath):
    sql = '''
        create table movie250
        (
        id integer primary key autoincrement,
        info_link text,
        pic_link text,
        cname varchar,
        ename varchar,
        score numeric,
        rated numeric,
        instroduction text,
        info text
        );
    '''
    conn = sqlite3.connect(dbpath)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    conn.close()

三、获取词云

  1. 读取数据库
  2. 使用jieba进行分割
  3. 使用word_length.txt存储词云长度
  4. 将原始图转成数组
  5. 使用ImageWordCloud初始化图片
  6. 使用pyplot生成和保存图片
def makeWordCloud():
    # 准备词云所需的词
    con = sqlite3.connect('movie.db')
    cur = con.cursor()
    sql = 'select instroduction from movie250'
    data = cur.execute(sql)
    text = ""
    for item in data:
        text = text + item[0]
    cur.close()
    con.close()

    cut = jieba.cut(text)
    string = ' '.join(cut)

    filename = 'word_length.txt'
    with open(filename, 'w') as file:
        file.write(str(len(string)))
        file.close()

    img = Image.open(r'../static/assets/img/tree.jpg')
    img_arry = np.array(img) # 将图片转换成数组
    wc = WordCloud(
        background_color = 'white',
        mask = img_arry,
        font_path = 'STCAIYUN.TTF' # 字体锁在位置: C:\Windows\Fonts
        )
    wc.generate_from_text(string)

    # 绘制图片
    fig = plt.figure(1)
    plt.imshow(wc)
    plt.axis('off') # 是否显示坐标轴
    # plt.show() # 显示生成的词云图片

    # 输出词云图片到文件
    plt.savefig(r'../static/assets/img/word.jpg', dpi=800)
    plt.close()

四、完成业务代码

# -*- coding:utf-8 -*-
 
# date: 2020-5-30
# author: jingluo
from flask import Flask, render_template,request, session
import get_douban_databses
import sqlite3
import os

# 分词
import jieba
# 绘图,数据可视化
from matplotlib import pyplot as plt
# 词云
from wordcloud import WordCloud
# 图片处理
from PIL import Image
# 矩阵运算
import numpy as np

# 自定义template路径
app = Flask(__name__,template_folder="../templates/",
    static_folder='../static/') #应用

# flask的session需要用到的秘钥字符串
app.config["SECRET_KEY"] = "akjsdhkjashdkjhaksk120191101asd"

@app.route("/")
def index():
    try:
        with open('word_length.txt', 'r') as file:
            word_length = file.readline()
            session['word_length'] = word_length
            file.close()
    except:
        word_length = 5633
        session['word_length'] = word_length
    return render_template("template/home.html",word_length = word_length)

@app.route("/home")
def home():
    word_length = session.get('word_length')
    return render_template("template/home.html",word_length = word_length)

@app.route("/movie")
def movie():
    movies = []
    con = sqlite3.connect("movie.db")
    cur = con.cursor()
    sql = "select * from movie250"
    data = cur.execute(sql)
    for item in data:
        movies.append(item)
    cur.close()
    con.close()
    return render_template("template/movie.html",movies = movies)

@app.route("/score")
def score():
    score = []
    number = []
    con = sqlite3.connect("movie.db")
    cur = con.cursor()
    sql = "select score,count(score) from movie250 group by score"
    data = cur.execute(sql)
    for item in data:
        score.append(item[0])
        number.append(item[1])
    cur.close()
    con.close()
    return render_template("template/score.html", score = score, number = number)

# 生成词云图片
def makeWordCloud():
    # 准备词云所需的词
    con = sqlite3.connect('movie.db')
    cur = con.cursor()
    sql = 'select instroduction from movie250'
    data = cur.execute(sql)
    text = ""
    for item in data:
        text = text + item[0]
    cur.close()
    con.close()

    cut = jieba.cut(text)
    string = ' '.join(cut)

    filename = 'word_length.txt'
    with open(filename, 'w') as file:
        file.write(str(len(string)))
        file.close()

    img = Image.open(r'../static/assets/img/tree.jpg')
    img_arry = np.array(img) # 将图片转换成数组
    wc = WordCloud(
        background_color = 'white',
        mask = img_arry,
        font_path = 'STCAIYUN.TTF' # 字体锁在位置: C:\Windows\Fonts
        )
    wc.generate_from_text(string)

    # 绘制图片
    fig = plt.figure(1)
    plt.imshow(wc)
    plt.axis('off') # 是否显示坐标轴
    # plt.show() # 显示生成的词云图片

    # 输出词云图片到文件
    plt.savefig(r'../static/assets/img/word.jpg', dpi=800)
    plt.close()

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

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

if __name__ == '__main__':
    app.config.update(DEBUG=True)
    if not os.path.exists('movie.db'):
        get_douban_databses.main()
    if not os.path.exists('../static/assets/img/word.jpg'):
        makeWordCloud()
    app.run()

五、使用教程

  1. git clone https://gitee.com/jingluoonline/python_reptile.git
  2. cd python_reptile/flsk/apps
  3. 输入创建虚拟化境的命令virtualenv FlaskPath
  4. 进入虚拟环境FlaskPath\Scripts\activate.bat
  5. 安装相关依赖
    1. 其中wordcloud下载有时候会有问题,可以选择使用whl文件下载,网址https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud找到相应的包下载到本地,进行本地安装
  6. python index.py,有点慢,因为爬取数据和生成图片都是在初始化时
  7. 浏览器输入http://127.0.0.1:5000/

六、效果图

  1. 主页
tQ5r8S.png
  1. 电影
tQ5HKJ.png
  1. 评分
tQ5OV1.png
  1. 词云
tQIeG8.png
  1. 团队
tQI8I0.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281