Python爬虫爬取电影票房数据及图表展示操作示例


Posted in Python onMarch 27, 2020

本文实例讲述了Python爬虫爬取电影票房数据及图表展示操作。分享给大家供大家参考,具体如下:

爬虫电影历史票房排行榜 http://www.cbooo.cn/BoxOffice/getInland?pIndex=1&t=0

  1. Python爬取历史电影票房纪录
  2. 解析Json数据
  3. 横向条形图展示
  4. 面向对象思想

导入相关库

import requests
import re
from matplotlib import pyplot as plt
from matplotlib import font_manager
import json

类代码部分

class DYOrder(object):
 #初始化
  def __init__(self,page=1):
    self.url = 'http://www.cbooo.cn/BoxOffice/getInland?pIndex={}&t=0'.format(page)
    self.headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'}
  #请求
  def __to_request(self):
    response = requests.get(url=self.url,headers=self.headers)
    return self.__to_parse(response.content.decode('utf-8'))
  #解析
  def __to_parse(self,html):
    #返回为JSON字符串
    #首先将字符串反序列化为JSON对象
    my_json = json.loads(html)
    return my_json
  #图表展示
  def __to_show(self,data,show_type):
    x = []
    y = []
    for value in data:
      x.append(value['MovieName'])
      y.append(int(value['BoxOffice']))
    
    my_font = font_manager.FontProperties(fname='/System/Library/Fonts/PingFang.ttc',size=18)
    
    if show_type == 1:
      plt.figure(figsize=(20,8),dpi=80)
      rects = plt.bar(range(len(x)),[float(i) for i in y],width=0.5,color='red')
      plt.xticks(range(len(x)),x,fontproperties=my_font,rotation=60)
      plt.xlabel('名称',rotation=60,color='blue',fontproperties=my_font)
      plt.ylabel('票房/万',rotation=60,color='blue',fontproperties=my_font)
      for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width()/2,height+0.4,str(height),ha='center',rotation=30)
    else:
      # 横向 plt.barh(y,x)
      plt.figure(figsize=(15,13),dpi=80)
      rects = plt.barh(range(len(x)),y,height=0.8,color='orange')
      plt.yticks(range(len(x)),x,fontproperties=my_font,rotation=30)
      plt.ylabel('名称',rotation=0,color='blue',fontproperties=my_font)
      plt.xlabel('票房/万',rotation=60,color='blue',fontproperties=my_font)
      for rect in rects:
        width = rect.get_width()
        plt.text(width, rect.get_y()+0.3/2,str(width),va='center',rotation=30)
  
    plt.grid(alpha=0.4)  
    plt.title('中国电影历史票房排行榜',color='red',size=18,fontproperties=my_font)
    plt.show()
  #所有操作
  def to_run(self,show_type=1):
    result = self.__to_request()
    self.__to_show(result,show_type)

调用类并展示

if __name__ == '__main__':
  dy_order = DYOrder(1)
  # type 1 竖向条形图 2 横向
  dy_order.to_run(2)

Python爬虫爬取电影票房数据及图表展示操作示例
Python爬虫爬取电影票房数据及图表展示操作示例

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
简单介绍Python中的floor()方法
May 15 Python
python选择排序算法实例总结
Jul 01 Python
利用python打印出菱形、三角形以及矩形的方法实例
Aug 08 Python
python机器学习案例教程——K最近邻算法的实现
Dec 28 Python
Python学习_几种存取xls/xlsx文件的方法总结
May 03 Python
Python实现的生产者、消费者问题完整实例
May 30 Python
3分钟学会一个Python小技巧
Nov 23 Python
解决使用PyCharm时无法启动控制台的问题
Jan 19 Python
Python制作词云图代码实例
Sep 09 Python
python针对Oracle常见查询操作实例分析
Apr 30 Python
Python如何安装第三方模块
May 28 Python
python palywright库基本使用
Jan 21 Python
Pyspark读取parquet数据过程解析
Mar 27 #Python
Python基于pyecharts实现关联图绘制
Mar 27 #Python
Python爬虫爬取杭州24时温度并展示操作示例
Mar 27 #Python
Django添加bootstrap框架时无法加载静态文件的解决方式
Mar 27 #Python
Python itertools.product方法代码实例
Mar 27 #Python
python实现图像全景拼接
Mar 27 #Python
如何在Python 游戏中模拟引力
Mar 27 #Python
You might like
基于php无限分类的深入理解
2013/06/02 PHP
PHP5.2下preg_replace函数的问题
2015/05/08 PHP
实现PHP框架系列文章(6)mysql数据库方法
2016/03/04 PHP
360搜索引擎自动收录php改写方案
2018/04/28 PHP
PHP 获取客户端 IP 地址的方法实例代码
2018/11/11 PHP
js中top/parent/frame概述及案例应用
2013/02/06 Javascript
javascript操作referer详细解析
2014/03/10 Javascript
JavaScript中的toUTCString()方法使用详解
2015/06/12 Javascript
在JavaScript的AngularJS库中进行单元测试的方法
2015/06/23 Javascript
jquery动态增加删减表格行特效
2015/11/20 Javascript
jQuery插件实现无缝滚动特效
2015/11/24 Javascript
jQuery如何使用自动触发事件trigger
2015/11/29 Javascript
Markdown与Bootstrap相结合实现图片自适应属性
2016/05/04 Javascript
javascript中递归的两种写法
2017/01/17 Javascript
十大 Node.js 的 Web 框架(快速提升工作效率)
2017/06/30 Javascript
JS实现分页导航效果
2020/02/19 Javascript
vue将文件/图片批量打包下载zip的教程
2020/10/21 Javascript
[02:51]DOTA2英雄基础教程 风暴之灵
2013/12/23 DOTA
[01:04:48]VGJ.S vs TNC Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
[36:20]KG vs SECRET 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
python生成随机密码或随机字符串的方法
2015/07/03 Python
Python实现脚本锁功能(同时只能执行一个脚本)
2017/05/10 Python
Python scikit-learn 做线性回归的示例代码
2017/11/01 Python
Python 中Pickle库的使用详解
2018/02/24 Python
PyQt5每天必学之滑块控件QSlider
2018/04/20 Python
python存储16bit和32bit图像的实例
2018/12/05 Python
对python实现二维函数高次拟合的示例详解
2018/12/29 Python
python中while和for的区别总结
2019/06/28 Python
python shell命令行中import多层目录下的模块操作
2020/03/09 Python
python文件路径操作方法总结
2020/12/21 Python
HTML5的download属性详细介绍和使用实例
2014/04/23 HTML / CSS
区域总监的岗位职责
2013/11/21 职场文书
2015年行政部工作总结
2015/04/28 职场文书
人与自然观后感
2015/06/16 职场文书
八年级作文之友谊
2019/12/02 职场文书
Python OpenCV形态学运算示例详解
2022/04/07 Python