Python爬虫以及图表展示

5 篇文章 0 订阅
3 篇文章 0 订阅

爬虫电影历史票房排行榜 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)

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码der

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值