最新下载
热门教程
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 
Python爬虫如何爬取电影票房数据 Python爬虫爬取电影票房数据代码及图表展示
时间:2022-11-14 23:01:52 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Python爬虫爬取电影票房数据代码,通过代码详细介绍了操作技巧以及实现方法,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
Python爬取历史电影票房纪录
解析Json数据
横向条形图展示
面向对象思想
导入相关库
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.*c*bo*oo.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],.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,.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)
	
 
	
 
相关文章
- 百度网盘SVIP激活码能用的有哪些 百度网盘vip免费领取 11-04
 - 打个螺丝兑换码能用的有哪些 2025最新有效兑换码汇总 11-04
 - 抓大鹅有效兑换码有哪些 2025最新可用兑换码大全 11-04
 - 密室出逃兑换码能用的有哪些 2025最新有效兑换码大全 11-04
 - 猪了个猪兑换码最新可用 2025最新有效兑换码汇总 11-04
 - 羊了个羊星球兑换码能用的有哪些 2025最新有效兑换码 11-04