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

作者:OldKind超 时间:2021-07-18 08:34:32 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/qq_36261130/article/details/100556017

标签:Python,爬虫,爬取
0
投稿

猜你喜欢

  • python中__slots__节约内存的具体做法

    2023-04-10 16:28:34
  • 浅析Python pandas模块输出每行中间省略号问题

    2022-03-27 21:26:03
  • 如何使用ASP实现网站的“目录树”管理

    2008-06-13 06:39:00
  • 网页表单项Input的高级限制级用法

    2008-10-27 16:50:00
  • ansible作为python模块库使用的方法实例

    2022-07-15 08:01:02
  • python pycurl验证basic和digest认证的方法

    2022-12-17 23:01:15
  • Django读取Mysql数据并显示在前端的实例

    2023-11-09 17:36:49
  • Msxml2.XMLHTTP Microsoft.XMLHTTP new XMLHttpRequest用法

    2010-03-30 09:43:00
  • 将后台数据从Berkeley的文件DB转到MySQL

    2009-01-04 13:31:00
  • MySQL优化之数据类型的使用

    2009-03-16 17:12:00
  • 利用Python实现K-Means聚类的方法实例(案例:用户分类)

    2023-05-15 13:14:34
  • php中使用session_set_save_handler()函数把session保存到MySQL数据库实例

    2023-11-18 01:11:16
  • 感知器基础原理及python实现过程详解

    2023-11-07 16:24:35
  • Python 2与Python 3版本和编码的对比

    2023-10-11 01:02:18
  • Go单元测试对GORM进行Mock测试

    2023-07-20 17:38:53
  • asp将table生成excel文件(xls)

    2011-03-07 11:17:00
  • pytorch教程resnet.py的实现文件源码分析

    2023-11-07 21:18:47
  • python Task在协程调用实例讲解

    2021-06-28 21:39:32
  • python二维图制作的实例代码

    2021-09-17 21:29:35
  • 教你如何在SQL Server计算机列和平均值

    2009-01-20 15:10:00
  • asp之家 网络编程 m.aspxhome.com