当前位置:   article > 正文

【Python应用】爬虫教程:利用python-Flask框架搭建本地数据可视化网站_使用爬虫来建一个网站

使用爬虫来建一个网站

目录

成果展示:

(1)网站首页:

(2)电影表单页

(3) 电影评分页

(4) 词频统计页

(5)团队页面

接下来让我们看看上述网站是如何完成的:首先简单介绍一下Flask框架:

Flask主要功能有两个:

1、环境搭建:

2、用Flask搭建第一个网络服务器程序——输出Hello,World!

3、开启Debug模式

 4、向网页中传递参数:

(1)string型

(2)int型

(3)float型

5、访问一个.HTML文件

(1)导入模块:

(2)创建文件夹:

(3)编写程序

6、向HTML网页中传递参数

(1)传入普通变量

        主程序(.py)

        HTML文件(.html)

(2)传入列表变量

(3)输出表格

(4)传入字典变量

 7、网页请求与响应

8、制作网站

(1)准备模板

(2)制作首页

(3)制作电影表单

(4)制作评分页面

(5)制作词频页面

(6)制作团队页面


 

成果展示:

(1)网站首页:

(2)电影表单页

(3) 电影评分页

(4) 词频统计

(5)团队页面

接下来让我们看看上述网站是如何完成的:
首先简单介绍一下Flask框架

Flask主要功能有两个:

(1)Werkzeug——路由转换

(2)jinja2——框架渲染 

1、环境搭建:

下载python-Flask扩展包

pip install flask

将Flask模块导入程序中:

from flask import Flask

2、用Flask搭建第一个网络服务器程序——输出Hello,World!

代码:

  1. from flask import Flask
  2. app = Flask(__name__)
  3. #路由解析:通过用户访问的路径,匹配相应函数
  4. @app.route('/')
  5. def h():
  6. return 'hello world'
  7. if __name__ == '__main__':
  8. app.run()

输出:

 

复制输出结果中的网址,然后在浏览器中打开,页面显示如下:

 

 成功输出“Hello World”

3、开启Debug模式

因为在制作网页中,我们要一边观察网页情况一边调试程序,所以每次都去点击打开和关闭程序按钮非常麻烦,所以为了方便debug,我们在程序中更改以下代码。

 app.run(debug=True)

完整:

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def h():
  5. return 'hello 1'
  6. if __name__ == '__main__':
  7. app.run(debug=True)

这样就能实时修改代码和网页了。

 4、向网页中传递参数:

(1)string型

路由传参,在网址路径中用<>将参数括起来,然后在访问函数中存入参数,并用占位符的方式显示参数。

  1. from flask import Flask
  2. app = Flask(__name__)
  3. #通过访问路径获取字符串参数
  4. @app.route("/test/<name>")
  5. def welcom(name):
  6. return "hello,%s"%name
  7. if __name__ == '__main__':
  8. app.run(debug=True)

输出: (*注意网址路径)

 

(2)int型

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route("/test/<int:id>")
  4. def welcom2(id):
  5. return "hello,%d的会员"%id
  6. if __name__ == '__main__':
  7. app.run(debug=True)

输出:

(3)float型

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route("/test/<float:v>")
  4. def welcom3(v):
  5. return "一共找您%.2f元钱"%v
  6. if __name__ == '__main__':
  7. app.run(debug=True)

输出:

也可以将以上例子中的函数放在一起,路由解析会自动判别你在网址传入的参数类型,并自动跳转到相应参数类型所在的函数网页。

5、访问一个.HTML文件

(1)导入模块:

        render_template

(2)创建文件夹:

        项目文件夹 ——> templates(新建)——> test.html

(3)编写程序

  1. from flask import Flask,render_template
  2. app = Flask(__name__)
  3. @app.route("/")
  4. def index2():
  5. return render_template("test.html")
  6. if __name__ == '__main__':
  7. app.run(debug=True)

输出:

6、向HTML网页中传递参数

(1)传入普通变量

        主程序(.py)

        在网页中显示日期。

        在html文件中,主程序传入的变量要用{{ xxx }}扣起来才能使用。

        脚本文件也是如此,但可能需要单独定义一个变量去赋值。var name = {{}}

  1. from flask import Flask,render_template
  2. import datetime
  3. app = Flask(__name__)
  4. @app.route("/time")
  5. def index():
  6. times = datetime.date.today()#普通变量
  7. return render_template("test2.html",var = times)
  8. if __name__ == '__main__':
  9. app.run(debug=True)

        HTML文件(.html)

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. 今天的时间是{{var}},Hello,HTML!
  9. </body>
  10. </html>

      输出:

(2)传入列表变量

主程序:

  1. from flask import Flask,render_template
  2. import datetime
  3. app=Flask(__name__)
  4. @app.route("/time")
  5. def index():
  6. time = datetime.date.today()#普通变量
  7. name = ["小王","小刘","小赵"]#列表类型
  8. return render_template("test3.html",var = time,list=name)
  9. if __name__ == '__main__':
  10. app.run(debug=True)

html文件:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. 今天的时间是{{var}},Hello HTML!<br />
  9. 今天值班的有:<br />
  10. {% for data in list %}<!--用大括号括起来的是控制结构,还有if-->
  11. <li> {{data}} </li><!--打印列表-->
  12. {% endfor %}
  13. </body>
  14. </html>

输出:

(3)输出表格

 html文件

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. 今天的时间是{{var}},Hello HTML!<br />
  9. 今天值班的有:<br />
  10. {% for data in list %}<!--用大括号括起来的是控制结构,还有if-->
  11. <li> {{data}} </li><!--打印列表-->
  12. {% endfor %}
  13. 任务:<br/><!--在页面打印表格与迭代-->
  14. <table border="1">
  15. <tr>
  16. <!--一行-->
  17. <td>第一列</td><!--一列-->
  18. <td>第二列</td><!--二列-->
  19. </tr>
  20. <tr>
  21. <!--一行-->
  22. <td>第一列</td><!--一列-->
  23. <td>第二列</td><!--二列-->
  24. </tr>
  25. <tr>
  26. <!--一行-->
  27. <td>第一列</td><!--一列-->
  28. <td>第二列</td><!--二列-->
  29. </tr>
  30. </table>
  31. </body>
  32. </html>

输出:

(4)传入字典变量

 主程序:

  1. from flask import Flask,render_template
  2. import datetime
  3. app=Flask(__name__)
  4. @app.route("/time")
  5. def index():
  6. time = datetime.date.today()#普通变量
  7. name = ["小王","小刘","小赵"]#列表类型
  8. task = {"任务":"打扫卫生","时间":"三个小时"}#字典类型
  9. return render_template("test3.html",var = time,list=name,task=task)
  10. if __name__ == '__main__':
  11. app.run(debug=True)

html:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. 今天的时间是{{var}},Hello HTML!<br />
  9. 今天值班的有:<br />
  10. {% for data in list %}<!--用大括号括起来的是控制结构,还有if-->
  11. <li> {{data}} </li><!--打印列表-->
  12. {% endfor %}
  13. 任务:<br/><!--在页面打印表格与迭代-->
  14. <table border="1">
  15. {% for key,value in task.items() %}<!--[(key,value),(key,value),(key,value)]-->
  16. <tr>
  17. <td>{{key}}</td>
  18. <td>{{value}}</td>
  19. </tr>
  20. {% endfor %}
  21. </table>
  22. </body>
  23. </html>

输出:

 7、网页请求与响应

在项目文件夹中新建文件夹test,在test中新建register.html和result.html两个HTML文件。

主程序:

  1. from flask import Flask,render_template
  2. import datetime
  3. app=Flask(__name__)
  4. #表单提交
  5. @app.route('/test/register')#网址
  6. def register():
  7. return render_template("test/register.html")#文件夹路径
  8. #接收到表单提交的路由,需要指定methods为post
  9. @app.route('/result',methods=['POST','GET'])
  10. def result():
  11. return render_template("test/result.html")
  12. if __name__ == '__main__':
  13. app.run(debug=True)

register.html

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. <form action="http://127.0.0.1:5000/result" method="post">
  9. <p>姓名:<input type="text" name="姓名"></p><!--{"姓名":"text"}-->
  10. <p>年龄:<input type="text" name="年龄"></p>
  11. <p>性别:<input type="text" name="性别"></p>
  12. <p>住址:<input type="text" name="住址"></p>
  13. <p><input type="submit" value="提交"></p>
  14. </form>
  15. </body>
  16. </html>

result.html

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. result
  9. </body>
  10. </html>

输出:

点击提交后:

将result页面改为数据表单:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. <table border="1">
  9. {% for key,value in result.items() %}
  10. <tr>
  11. <th>{{key}}</th>
  12. <td>{{value}}</td>
  13. </tr>
  14. {% endfor %}
  15. </table>
  16. </body>
  17. </html>

输出结果:

注意:

  在上面的register.html代码中,网址响应部分是我们固定写好的,如果我们想让他自己去填写路径应该这么做:

动态访问页面:

url_for("访问路由名字")

返回的是'/result'

 <form action="{{url_for('result')}}" method="post">

8、制作网站

(1)准备模板

模板网站:

网页模板,网站模板,DIV+CSS模板,企业网站模板下载-模板之家 (cssmoban.com)icon-default.png?t=N7T8http://www.cssmoban.com/

示例模板:

在线模拟预览-模板之家cssMoban.comicon-default.png?t=N7T8http://www.cssmoban.com/preview/index.html?url=http://demo.mxyhn.xyz:8020/cssthemes6/btmd_12_Mamba/index.html&id=9296&tid=20136294750679

(淘宝1毛钱一个)

下载完的网站文件夹:

 1、把assets和index.html复制到我们的项目文件夹。

2、将html文件放入项目中的templates文件夹。(最好copy一份做备份。)

3、新建一个static文件夹用来存放资源文件。(将asset放入static文件中。)

准备工作做好了,接下来开始制作网页!

提供一个图标网站(以后可能会用到):

iconfont-阿里巴巴矢量图标库

矢量图的使用方法:

iconfont字体图标的使用方法--超简单! - 全堆栈溢出攻城狮 - 博客园 (cnblogs.com)

(2)制作首页

index.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta content="width=device-width, initial-scale=1.0" name="viewport">
  6. <title>豆瓣Top250数据可视化分析</title>
  7. <meta content="" name="descriptison">
  8. <meta content="" name="keywords">
  9. <!-- Favicons -->
  10. <link href="static/assets/img/favicon.png" rel="icon">
  11. <link href="static/assets/img/apple-touch-icon.png" rel="apple-touch-icon">
  12. <!-- Google Fonts -->
  13. <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,600,600i,700,700i,900" rel="stylesheet">
  14. <!-- Vendor CSS Files -->
  15. <link href="static/assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  16. <link href="static/assets/vendor/icofont/icofont.min.css" rel="stylesheet">
  17. <link href="static/assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  18. <link href="static/assets/vendor/animate.css/animate.min.css" rel="stylesheet">
  19. <link href="static/assets/vendor/venobox/venobox.css" rel="stylesheet">
  20. <link href="static/assets/vendor/aos/aos.css" rel="stylesheet">
  21. <!-- Template Main CSS File -->
  22. <link href="static/assets/css/style.css" rel="stylesheet">
  23. </head>
  24. <body>
  25. <!-- ======= Header ======= -->
  26. <header id="header">
  27. <div class="container">
  28. <div class="logo float-left">
  29. <h1 class="text-light"><a href="/index"><span>DouBan</span></a></h1>
  30. <!-- Uncomment below if you prefer to use an image logo -->
  31. <!-- <a href="index.html"><img src="static/assets/img/logo.png" alt="" class="img-fluid"></a>-->
  32. </div>
  33. <nav class="nav-menu float-right d-none d-lg-block">
  34. <ul>
  35. <li class="active"><a href="/index">首页 <i class="la la-angle-down"></i></a></li>
  36. <li><a href="/movie">电影</a></li>
  37. <li><a href="/score">评分</a></li>
  38. <li><a href="/word">词云</a></li>
  39. <li><a href="/team">团队</a></li>
  40. </ul>
  41. </nav><!-- .nav-menu -->
  42. </div>
  43. </header><!-- End Header -->
  44. <!-- ======= Our Team Section ======= -->
  45. <section id="team" class="team">
  46. <div class="container">
  47. <div class="section-title">
  48. <h2>豆瓣Top250数据可视化分析</h2>
  49. <p>应用python爬虫、Flask框架、Echarts、WordCloud等技术实现</p>
  50. </div>
  51. <!-- ======= Counts Section ======= -->
  52. <section class="counts section-bg">
  53. <div class="container">
  54. <div class="row">
  55. <div class="col-lg-3 col-md-6 text-center" data-aos="fade-up">
  56. <a href="/movie">
  57. <div class="count-box">
  58. <i class="icofont-simple-smile" style="color: #20b38e;"></i>
  59. <span data-toggle="counter-up">250</span>
  60. <p>经典电影</p>
  61. </div>
  62. </a>
  63. </div>
  64. <div class="col-lg-3 col-md-6 text-center" data-aos="fade-up" data-aos-delay="200">
  65. <a href="/score">
  66. <div class="count-box">
  67. <i class="icofont-document-folder" style="color: #c042ff;"></i>
  68. <span data-toggle="counter-up">521</span>
  69. <p>评分报告</p>
  70. </div>
  71. </a>
  72. </div>
  73. <div class="col-lg-3 col-md-6 text-center" data-aos="fade-up" data-aos-delay="400">
  74. <a href="/word">
  75. <div class="count-box">
  76. <i class="icofont-live-support" style="color: #46d1ff;"></i>
  77. <span data-toggle="counter-up">1,463</span>
  78. <p>词频统计</p>
  79. </div>
  80. </a>
  81. </div>
  82. <div class="col-lg-3 col-md-6 text-center" data-aos="fade-up" data-aos-delay="600">
  83. <a href="/team">
  84. <div class="count-box">
  85. <i class="icofont-users-alt-5" style="color: #ffb459;"></i>
  86. <span data-toggle="counter-up">15</span>
  87. <p>团队成员</p>
  88. </div>
  89. </a>
  90. </div>
  91. </div>
  92. </div>
  93. </section><!-- End Counts Section -->
  94. </div>
  95. </section><!-- End Our Team Section -->
  96. <!-- ======= Footer ======= -->
  97. <footer id="footer">
  98. <div class="container">
  99. <div class="copyright">
  100. &copy; python爬虫可视化分析实验
  101. </div>
  102. <div class="credits">
  103. Douban Movie Top <a href="https://movie.douban.com/top250" target="_blank" title="豆瓣Top250">豆瓣Top250</a> - Douban Movie <a href="https://movie.douban.com/" title="豆瓣电影" target="_blank">豆瓣电影</a>
  104. </div>
  105. </div>
  106. </footer><!-- End Footer -->
  107. <a href="#" class="back-to-top"><i class="icofont-simple-up"></i></a>
  108. <!-- Vendor JS Files -->
  109. <script src="static/assets/vendor/jquery/jquery.min.js"></script>
  110. <script src="static/assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  111. <script src="static/assets/vendor/jquery.easing/jquery.easing.min.js"></script>
  112. <script src="static/assets/vendor/php-email-form/validate.js"></script>
  113. <script src="static/assets/vendor/jquery-sticky/jquery.sticky.js"></script>
  114. <script src="static/assets/vendor/venobox/venobox.min.js"></script>
  115. <script src="static/assets/vendor/waypoints/jquery.waypoints.min.js"></script>
  116. <script src="static/assets/vendor/counterup/counterup.min.js"></script>
  117. <script src="static/assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
  118. <script src="static/assets/vendor/aos/aos.js"></script>
  119. <!-- Template Main JS File -->
  120. <script src="static/assets/js/main.js"></script>
  121. <style>
  122. .copyrights {
  123. text-indent: -9999px;
  124. height: 0;
  125. line-height: 0;
  126. font-size: 0;
  127. overflow: hidden;
  128. }
  129. </style>
  130. <div class="copyrights" id="links20210126">
  131. Collect from <a href="http://www.cssmoban.com/" title="网站模板">模板之家</a>
  132. <a href="https://www.chazidian.com/" title="查字典">查字典</a>
  133. </div>
  134. </body>
  135. </html>

主程序:

  1. from flask import Flask,render_template
  2. import sqlite3
  3. app = Flask(__name__)
  4. #路由解析:通过用户访问的路径,匹配相应函数
  5. @app.route('/')
  6. def index():
  7. return render_template("index.html")
  8. #返回首页
  9. @app.route('/index')
  10. def home():
  11. return render_template("index.html")
  12. #返回电影页面
  13. @app.route('/movie')
  14. def movie():
  15. datalist = []
  16. con = sqlite3.connect("moveTop.db")#打开数据库
  17. cur = con.cursor()#获取游标
  18. sql = "select * from movieTop250"#sql查询语句
  19. data = cur.execute(sql)#获取数据
  20. for item in data:#将数据保存在列表中
  21. datalist.append(item)
  22. cur.close()#关闭游标
  23. con.close()#关闭连接
  24. return render_template("movie.html",movies = datalist)
  25. #返回评分界面
  26. @app.route('/score')
  27. def score():
  28. return render_template("score.html")
  29. #返回词云界面
  30. @app.route('/word')
  31. def word():
  32. return render_template("word.html")
  33. #返回团队界面
  34. @app.route('/team')
  35. def team():
  36. return render_template("team.html")
  37. if __name__ == '__main__':
  38. app.run(debug=True)

页面展示: 

(3)制作电影表单

        数据库相关操作看上一个教程:爬虫保姆级教程2:在visual studio中使用python项目连接SQLite数据库--爬虫数据保存之数据库_代码骑士的博客-CSDN博客

  1. #引入数据库
  2. import sqlite3

 movie函数:

  1. #返回电影页面
  2. @app.route('/movie')
  3. def movie():
  4. datalist = []
  5. con = sqlite3.connect("moveTop.db")#打开数据库
  6. cur = con.cursor()#获取游标
  7. sql = "select * from movieTop250"#sql查询语句
  8. data = cur.execute(sql)#获取数据
  9. for item in data:#将数据保存在列表中
  10. datalist.append(item)
  11. cur.close()#关闭游标
  12. con.close()#关闭连接
  13. return render_template("movie.html",movies = datalist)

 movie.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta content="width=device-width, initial-scale=1.0" name="viewport">
  6. <title>豆瓣Top250数据可视化分析</title>
  7. <meta content="" name="descriptison">
  8. <meta content="" name="keywords">
  9. <!-- Favicons -->
  10. <link href="static/assets/img/favicon.png" rel="icon">
  11. <link href="static/assets/img/apple-touch-icon.png" rel="apple-touch-icon">
  12. <!-- Google Fonts -->
  13. <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,600,600i,700,700i,900" rel="stylesheet">
  14. <!-- Vendor CSS Files -->
  15. <link href="static/assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  16. <link href="static/assets/vendor/icofont/icofont.min.css" rel="stylesheet">
  17. <link href="static/assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  18. <link href="static/assets/vendor/animate.css/animate.min.css" rel="stylesheet">
  19. <link href="static/assets/vendor/venobox/venobox.css" rel="stylesheet">
  20. <link href="static/assets/vendor/aos/aos.css" rel="stylesheet">
  21. <!-- Template Main CSS File -->
  22. <link href="static/assets/css/style.css" rel="stylesheet">
  23. </head>
  24. <body>
  25. <!-- ======= Header ======= -->
  26. <header id="header">
  27. <div class="container">
  28. <div class="logo float-left">
  29. <h1 class="text-light"><a href="/index"><span>DouBan</span></a></h1>
  30. <!-- Uncomment below if you prefer to use an image logo -->
  31. <!-- <a href="index.html"><img src="static/assets/img/logo.png" alt="" class="img-fluid"></a>-->
  32. </div>
  33. <nav class="nav-menu float-right d-none d-lg-block">
  34. <ul>
  35. <li class="active"><a href="/index">首页 <i class="la la-angle-down"></i></a></li>
  36. <li><a href="/movie">电影</a></li>
  37. <li><a href="/score">评分</a></li>
  38. <li><a href="/word">词云</a></li>
  39. <li><a href="/team">团队</a></li>
  40. </ul>
  41. </nav><!-- .nav-menu -->
  42. </div>
  43. </header><!-- End Header -->
  44. <!-- ======= Our Team Section ======= -->
  45. <section id="team" class="team">
  46. <div class="container">
  47. <div class="section-title">
  48. <h2>豆瓣Top250电影</h2>
  49. </div>
  50. <!-- ======= Counts Section ======= -->
  51. <section class="counts section-bg">
  52. <div class="container">
  53. <table class="table table-striped">
  54. <tr>
  55. <td>电影排名</td>
  56. <td>中文名称</td>
  57. <td>外国名称</td>
  58. <td>电影评分</td>
  59. <td>评价人数</td>
  60. <td>电影概述</td>
  61. <td>其他信息</td>
  62. </tr>
  63. {% for movie in movies %}
  64. <tr>
  65. <td>{{ movie[0] }}</td>
  66. <td>
  67. <a href="{{ movie[1] }}">
  68. {{ movie[3] }}
  69. </a>
  70. </td>
  71. <td>{{ movie[4] }}</td>
  72. <td>{{ movie[5] }}</td>
  73. <td>{{ movie[6] }}</td>
  74. <td>{{ movie[7] }}</td>
  75. <td>{{ movie[8] }}</td>
  76. </tr>
  77. {% endfor %}
  78. </table>
  79. </div>
  80. </section><!-- End Counts Section -->
  81. </div>
  82. </section><!-- End Our Team Section -->
  83. <!-- ======= Footer ======= -->
  84. <footer id="footer">
  85. <div class="container">
  86. <div class="copyright">
  87. &copy; python爬虫可视化分析实验
  88. </div>
  89. <div class="credits">
  90. Douban Movie Top <a href="https://movie.douban.com/top250" target="_blank" title="豆瓣Top250">豆瓣Top250</a> - Douban Movie <a href="https://movie.douban.com/" title="豆瓣电影" target="_blank">豆瓣电影</a>
  91. </div>
  92. </div>
  93. </footer><!-- End Footer -->
  94. <a href="#" class="back-to-top"><i class="icofont-simple-up"></i></a>
  95. <!-- Vendor JS Files -->
  96. <script src="static/assets/vendor/jquery/jquery.min.js"></script>
  97. <script src="static/assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  98. <script src="static/assets/vendor/jquery.easing/jquery.easing.min.js"></script>
  99. <script src="static/assets/vendor/php-email-form/validate.js"></script>
  100. <script src="static/assets/vendor/jquery-sticky/jquery.sticky.js"></script>
  101. <script src="static/assets/vendor/venobox/venobox.min.js"></script>
  102. <script src="static/assets/vendor/waypoints/jquery.waypoints.min.js"></script>
  103. <script src="static/assets/vendor/counterup/counterup.min.js"></script>
  104. <script src="static/assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
  105. <script src="static/assets/vendor/aos/aos.js"></script>
  106. <!-- Template Main JS File -->
  107. <script src="static/assets/js/main.js"></script>
  108. <style>
  109. .copyrights {
  110. text-indent: -9999px;
  111. height: 0;
  112. line-height: 0;
  113. font-size: 0;
  114. overflow: hidden;
  115. }
  116. </style>
  117. <div class="copyrights" id="links20210126">
  118. Collect from <a href="http://www.cssmoban.com/" title="网站模板">模板之家</a>
  119. <a href="https://www.chazidian.com/" title="查字典">查字典</a>
  120. </div>
  121. </body>
  122. </html>

页面展示:

(4)制作评分页面

Echarts官方网站:

(官网很多图表示例,推荐大家去学习。)

Apache ECharts

下载配置echarts

Echarts扩展包:

链接:https://pan.baidu.com/s/1z07VwLkkjJW1ZYdCVOhKNw 
提取码:pc15

网站模板素材:

链接:https://pan.baidu.com/s/1D_PEc5c32qM8O1MEcRVLww 
提取码:gwxs

放入位置:static-->assets-->js 

score.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta content="width=device-width, initial-scale=1.0" name="viewport">
  6. <title>豆瓣Top250数据可视化分析</title>
  7. <meta content="" name="descriptison">
  8. <meta content="" name="keywords">
  9. <!-- Favicons -->
  10. <link href="static/assets/img/favicon.png" rel="icon">
  11. <link href="static/assets/img/apple-touch-icon.png" rel="apple-touch-icon">
  12. <!-- Google Fonts -->
  13. <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,600,600i,700,700i,900" rel="stylesheet">
  14. <!-- Vendor CSS Files -->
  15. <link href="static/assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  16. <link href="static/assets/vendor/icofont/icofont.min.css" rel="stylesheet">
  17. <link href="static/assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  18. <link href="static/assets/vendor/animate.css/animate.min.css" rel="stylesheet">
  19. <link href="static/assets/vendor/venobox/venobox.css" rel="stylesheet">
  20. <link href="static/assets/vendor/aos/aos.css" rel="stylesheet">
  21. <!-- Template Main CSS File -->
  22. <link href="static/assets/css/style.css" rel="stylesheet">
  23. </head>
  24. <body>
  25. <!-- ======= Header ======= -->
  26. <header id="header">
  27. <div class="container">
  28. <div class="logo float-left">
  29. <h1 class="text-light"><a href="/index"><span>DouBan</span></a></h1>
  30. <!-- Uncomment below if you prefer to use an image logo -->
  31. <!-- <a href="index.html"><img src="static/assets/img/logo.png" alt="" class="img-fluid"></a>-->
  32. </div>
  33. <nav class="nav-menu float-right d-none d-lg-block">
  34. <ul>
  35. <li class="active"><a href="/index">首页 <i class="la la-angle-down"></i></a></li>
  36. <li><a href="/movie">电影</a></li>
  37. <li><a href="/score">评分</a></li>
  38. <li><a href="/word">词云</a></li>
  39. <li><a href="/team">团队</a></li>
  40. </ul>
  41. </nav><!-- .nav-menu -->
  42. </div>
  43. </header><!-- End Header -->
  44. <!-- ======= Our Team Section ======= -->
  45. <section id="team" class="team">
  46. <div class="container">
  47. <div class="section-title">
  48. <h2>豆瓣Top250评分分布图</h2>
  49. </div>
  50. <!-- ======= Counts Section ======= -->
  51. <section class="counts section-bg">
  52. <div class="container">
  53. <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
  54. <div id="main" style="width: 100%;height:400px;"></div>
  55. </div>
  56. </section><!-- End Counts Section -->
  57. </div>
  58. </section><!-- End Our Team Section -->
  59. <!-- ======= Footer ======= -->
  60. <footer id="footer">
  61. <div class="container">
  62. <div class="copyright">
  63. &copy; python爬虫可视化分析实验
  64. </div>
  65. <div class="credits">
  66. Douban Movie Top <a href="https://movie.douban.com/top250" target="_blank" title="豆瓣Top250">豆瓣Top250</a> - Douban Movie <a href="https://movie.douban.com/" title="豆瓣电影" target="_blank">豆瓣电影</a>
  67. </div>
  68. </div>
  69. </footer><!-- End Footer -->
  70. <a href="#" class="back-to-top"><i class="icofont-simple-up"></i></a>
  71. <!-- Vendor JS Files -->
  72. <script src="static/assets/vendor/jquery/jquery.min.js"></script>
  73. <script src="static/assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  74. <script src="static/assets/vendor/jquery.easing/jquery.easing.min.js"></script>
  75. <script src="static/assets/vendor/jquery-sticky/jquery.sticky.js"></script>
  76. <script src="static/assets/vendor/venobox/venobox.min.js"></script>
  77. <script src="static/assets/vendor/waypoints/jquery.waypoints.min.js"></script>
  78. <script src="static/assets/vendor/counterup/counterup.min.js"></script>
  79. <script src="static/assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
  80. <script src="static/assets/vendor/aos/aos.js"></script>
  81. <script src="static/assets/js/echarts.min.js"></script>
  82. <!-- 引入刚刚下载的 ECharts 文件 -->
  83. <script type="text/javascript">
  84. var dom = document.getElementById("main");
  85. var myChart = echarts.init(dom);
  86. var app = {};
  87. var option;
  88. var datascore = [];
  89. datascore = {{ score }};
  90. var datanum = [];
  91. datanum = {{ num }};
  92. option = {
  93. title: {
  94. text: '电影评分表',
  95. subtext: 'Film rating sheet'
  96. },
  97. color: ['#3398DB'],
  98. tooltip: {
  99. trigger: 'axis',
  100. axisPointer: {
  101. type: 'shadow'
  102. }
  103. },
  104. xAxis: {
  105. type: 'category',
  106. data: datascore
  107. },
  108. yAxis: {
  109. type: 'value'
  110. },
  111. series: [
  112. {
  113. data: datanum,
  114. type: 'bar',
  115. barWidth: '60%',
  116. }
  117. ]
  118. };
  119. if (option && typeof option === 'object') {
  120. myChart.setOption(option);
  121. }
  122. </script>
  123. <!-- Template Main JS File -->
  124. <script src="static/assets/js/main.js"></script>
  125. <style>
  126. .copyrights {
  127. text-indent: -9999px;
  128. height: 0;
  129. line-height: 0;
  130. font-size: 0;
  131. overflow: hidden;
  132. }
  133. </style>
  134. <div class="copyrights" id="links20210126">
  135. Collect from <a href="http://www.cssmoban.com/" title="网站模板">模板之家</a>
  136. <a href="https://www.chazidian.com/" title="查字典">查字典</a>
  137. </div>
  138. </body>
  139. </html>

主程序:

  1. #返回评分界面
  2. @app.route('/score')
  3. def score():
  4. score=[]
  5. num=[]
  6. con = sqlite3.connect("moveTop.db")#打开数据库
  7. cur = con.cursor()#获取游标
  8. sql = "select score,count(score) from movieTop250 group by score"#sql查询语句
  9. data = cur.execute(sql)#获取数据
  10. for item in data:#将数据保存在列表中
  11. score.append(item[0])
  12. num.append(item[1])
  13. cur.close()#关闭游标
  14. con.close()#关闭连接
  15. return render_template("score.html",score = score,num = num)

 页面展示:

(5)制作词频页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta content="width=device-width, initial-scale=1.0" name="viewport">
  6. <title>豆瓣Top250数据可视化分析</title>
  7. <meta content="" name="descriptison">
  8. <meta content="" name="keywords">
  9. <!-- Favicons -->
  10. <link href="static/assets/img/favicon.png" rel="icon">
  11. <link href="static/assets/img/apple-touch-icon.png" rel="apple-touch-icon">
  12. <!-- Google Fonts -->
  13. <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,600,600i,700,700i,900" rel="stylesheet">
  14. <!-- Vendor CSS Files -->
  15. <link href="static/assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  16. <link href="static/assets/vendor/icofont/icofont.min.css" rel="stylesheet">
  17. <link href="static/assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  18. <link href="static/assets/vendor/animate.css/animate.min.css" rel="stylesheet">
  19. <link href="static/assets/vendor/venobox/venobox.css" rel="stylesheet">
  20. <link href="static/assets/vendor/aos/aos.css" rel="stylesheet">
  21. <!-- Template Main CSS File -->
  22. <link href="static/assets/css/style.css" rel="stylesheet">
  23. </head>
  24. <body>
  25. <!-- ======= Header ======= -->
  26. <header id="header">
  27. <div class="container">
  28. <div class="logo float-left">
  29. <h1 class="text-light"><a href="/index"><span>DouBan</span></a></h1>
  30. <!-- Uncomment below if you prefer to use an image logo -->
  31. <!-- <a href="index.html"><img src="static/assets/img/logo.png" alt="" class="img-fluid"></a>-->
  32. </div>
  33. <nav class="nav-menu float-right d-none d-lg-block">
  34. <ul>
  35. <li class="active"><a href="/index">首页 <i class="la la-angle-down"></i></a></li>
  36. <li><a href="/movie">电影</a></li>
  37. <li><a href="/score">评分</a></li>
  38. <li><a href="/word">词云</a></li>
  39. <li><a href="/team">团队</a></li>
  40. </ul>
  41. </nav><!-- .nav-menu -->
  42. </div>
  43. </header><!-- End Header -->
  44. <!-- ======= About Us Section ======= -->
  45. <section id="about" class="about">
  46. <div class="container">
  47. <div class="row no-gutters">
  48. <div class="col-lg-6 video-box">
  49. <img src="static/assets/img/about.jpg" class="img-fluid" alt="">
  50. </div>
  51. <div class="col-lg-6 d-flex flex-column justify-content-center about-content">
  52. <div class="section-title">
  53. <h2>词频统计</h2>
  54. <p>根据250部电影的一句话描述,提炼出词云树,可以让我们更加清晰的了解人们对经典电影的理解。</p>
  55. </div>
  56. <div class="icon-box" data-aos="fade-up" data-aos-delay="100">
  57. <div class="icon"><i class="bx bx-fingerprint"></i></div>
  58. <h4 class="title"><a href="">关于电影</a></h4>
  59. <p class="description">你有什么样的感悟?</p>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. </section><!-- End About Us Section -->
  65. <!-- ======= Footer ======= -->
  66. <footer id="footer">
  67. <div class="container">
  68. <div class="copyright">
  69. &copy; python爬虫可视化分析实验
  70. </div>
  71. <div class="credits">
  72. Douban Movie Top <a href="https://movie.douban.com/top250" target="_blank" title="豆瓣Top250">豆瓣Top250</a> - Douban Movie <a href="https://movie.douban.com/" title="豆瓣电影" target="_blank">豆瓣电影</a>
  73. </div>
  74. </div>
  75. </footer><!-- End Footer -->
  76. <a href="#" class="back-to-top"><i class="icofont-simple-up"></i></a>
  77. <!-- Vendor JS Files -->
  78. <script src="static/assets/vendor/jquery/jquery.min.js"></script>
  79. <script src="static/assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  80. <script src="static/assets/vendor/jquery.easing/jquery.easing.min.js"></script>
  81. <script src="static/assets/vendor/php-email-form/validate.js"></script>
  82. <script src="static/assets/vendor/jquery-sticky/jquery.sticky.js"></script>
  83. <script src="static/assets/vendor/venobox/venobox.min.js"></script>
  84. <script src="static/assets/vendor/waypoints/jquery.waypoints.min.js"></script>
  85. <script src="static/assets/vendor/counterup/counterup.min.js"></script>
  86. <script src="static/assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
  87. <script src="static/assets/vendor/aos/aos.js"></script>
  88. <!-- Template Main JS File -->
  89. <script src="static/assets/js/main.js"></script>
  90. <style>
  91. .copyrights {
  92. text-indent: -9999px;
  93. height: 0;
  94. line-height: 0;
  95. font-size: 0;
  96. overflow: hidden;
  97. }
  98. </style>
  99. <div class="copyrights" id="links20210126">
  100. Collect from <a href="http://www.cssmoban.com/" title="网站模板">模板之家</a>
  101. <a href="https://www.chazidian.com/" title="查字典">查字典</a>
  102. </div>
  103. </body>
  104. </html>

页面展示:

(6)制作团队页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta content="width=device-width, initial-scale=1.0" name="viewport">
  6. <title>豆瓣Top250数据可视化分析</title>
  7. <meta content="" name="descriptison">
  8. <meta content="" name="keywords">
  9. <!-- Favicons -->
  10. <link href="static/assets/img/favicon.png" rel="icon">
  11. <link href="static/assets/img/apple-touch-icon.png" rel="apple-touch-icon">
  12. <!-- Google Fonts -->
  13. <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,600,600i,700,700i,900" rel="stylesheet">
  14. <!-- Vendor CSS Files -->
  15. <link href="static/assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  16. <link href="static/assets/vendor/icofont/icofont.min.css" rel="stylesheet">
  17. <link href="static/assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  18. <link href="static/assets/vendor/animate.css/animate.min.css" rel="stylesheet">
  19. <link href="static/assets/vendor/venobox/venobox.css" rel="stylesheet">
  20. <link href="static/assets/vendor/aos/aos.css" rel="stylesheet">
  21. <!-- Template Main CSS File -->
  22. <link href="static/assets/css/style.css" rel="stylesheet">
  23. </head>
  24. <body>
  25. <!-- ======= Header ======= -->
  26. <header id="header">
  27. <div class="container">
  28. <div class="logo float-left">
  29. <h1 class="text-light"><a href="/index"><span>DouBan</span></a></h1>
  30. <!-- Uncomment below if you prefer to use an image logo -->
  31. <!-- <a href="index.html"><img src="static/assets/img/logo.png" alt="" class="img-fluid"></a>-->
  32. </div>
  33. <nav class="nav-menu float-right d-none d-lg-block">
  34. <ul>
  35. <li class="active"><a href="/index">首页 <i class="la la-angle-down"></i></a></li>
  36. <li><a href="/movie">电影</a></li>
  37. <li><a href="/score">评分</a></li>
  38. <li><a href="/word">词云</a></li>
  39. <li><a href="/team">团队</a></li>
  40. </ul>
  41. </nav><!-- .nav-menu -->
  42. </div>
  43. </header><!-- End Header -->
  44. <!-- ======= Our Team Section ======= -->
  45. <section id="team" class="team">
  46. <div class="container">
  47. <div class="section-title">
  48. <h2>我们的团队</h2>
  49. </div>
  50. <div class="row">
  51. <div class="col-xl-3 col-lg-4 col-md-6" data-aos="fade-up">
  52. <div class="member">
  53. <div class="pic">
  54. <a href="https://blog.csdn.net/qq_51701007?spm=1000.2115.3001.5343">
  55. <img src="static/assets/img/team/team-1.jpg" class="img-fluid" alt="">
  56. </a>
  57. </div>
  58. <div class="member-info">
  59. <h4>代码骑士</h4>
  60. <span>QQ:1696297834</span>
  61. </div>
  62. </div>
  63. </div>
  64. <div class="col-xl-3 col-lg-4 col-md-6" data-aos="fade-up" data-aos-delay="100">
  65. <div class="member">
  66. <div class="pic">
  67. <a href="https://blog.csdn.net/qq_51701007?spm=1000.2115.3001.5343">
  68. <img src="static/assets/img/team/team-2.jpg" class="img-fluid" alt="">
  69. </a>
  70. </div>
  71. <div class="member-info">
  72. <h4>代码骑士</h4>
  73. <span>QQ:1696297834</span>
  74. </div>
  75. </div>
  76. </div>
  77. <div class="col-xl-3 col-lg-4 col-md-6" data-aos="fade-up" data-aos-delay="200">
  78. <div class="member">
  79. <div class="pic">
  80. <a href="https://blog.csdn.net/qq_51701007?spm=1000.2115.3001.5343">
  81. <img src="static/assets/img/team/team-3.jpg" class="img-fluid" alt="">
  82. </a>
  83. </div>
  84. <div class="member-info">
  85. <h4>代码骑士</h4>
  86. <span>QQ:1696297834</span>
  87. </div>
  88. </div>
  89. </div>
  90. <div class="col-xl-3 col-lg-4 col-md-6" data-aos="fade-up" data-aos-delay="300">
  91. <div class="member">
  92. <div class="pic">
  93. <a href="https://blog.csdn.net/qq_51701007?spm=1000.2115.3001.5343">
  94. <img src="static/assets/img/team/team-4.jpg" class="img-fluid" alt="">
  95. </a>
  96. </div>
  97. <div class="member-info">
  98. <h4>代码骑士</h4>
  99. <span>QQ:1696297834</span>
  100. </div>
  101. </div>
  102. </div>
  103. </div>
  104. </div>
  105. </section><!-- End Our Team Section -->
  106. <!-- ======= Footer ======= -->
  107. <footer id="footer">
  108. <div class="container">
  109. <div class="copyright">
  110. &copy; python爬虫可视化分析实验
  111. </div>
  112. <div class="credits">
  113. Douban Movie Top <a href="https://movie.douban.com/top250" target="_blank" title="豆瓣Top250">豆瓣Top250</a> - Douban Movie <a href="https://movie.douban.com/" title="豆瓣电影" target="_blank">豆瓣电影</a>
  114. </div>
  115. </div>
  116. </footer><!-- End Footer -->
  117. <a href="#" class="back-to-top"><i class="icofont-simple-up"></i></a>
  118. <!-- Vendor JS Files -->
  119. <script src="static/assets/vendor/jquery/jquery.min.js"></script>
  120. <script src="static/assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  121. <script src="static/assets/vendor/jquery.easing/jquery.easing.min.js"></script>
  122. <script src="static/assets/vendor/jquery-sticky/jquery.sticky.js"></script>
  123. <script src="static/assets/vendor/venobox/venobox.min.js"></script>
  124. <script src="static/assets/vendor/waypoints/jquery.waypoints.min.js"></script>
  125. <script src="static/assets/vendor/counterup/counterup.min.js"></script>
  126. <script src="static/assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
  127. <script src="static/assets/vendor/aos/aos.js"></script>
  128. <!-- Template Main JS File -->
  129. <script src="static/assets/js/main.js"></script>
  130. <style>
  131. .copyrights {
  132. text-indent: -9999px;
  133. height: 0;
  134. line-height: 0;
  135. font-size: 0;
  136. overflow: hidden;
  137. }
  138. </style>
  139. <div class="copyrights" id="links20210126">
  140. Collect from <a href="http://www.cssmoban.com/" title="网站模板">模板之家</a>
  141. <a href="https://www.chazidian.com/" title="查字典">查字典</a>
  142. </div>
  143. </body>
  144. </html>

 页面展示:

大功告成啦! 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/294399
推荐阅读
相关标签
  

闽ICP备14008679号