Python爬蟲(chóng)爬取電影票房數(shù)據(jù)及圖表展示操作示例
本文實(shí)例講述了Python爬蟲(chóng)爬取電影票房數(shù)據(jù)及圖表展示操作。分享給大家供大家參考,具體如下:
爬蟲(chóng)電影歷史票房排行榜 http://www.cbooo.cn/BoxOffice/getInland?pIndex=1&t=0
- Python爬取歷史電影票房紀(jì)錄
- 解析Json數(shù)據(jù)
- 橫向條形圖展示
- 面向?qū)ο笏枷?/li>
導(dǎo)入相關(guān)庫(kù)
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'} #請(qǐng)求 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對(duì)象 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('票房/萬(wàn)',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('票房/萬(wàn)',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('中國(guó)電影歷史票房排行榜',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)
調(diào)用類并展示
if __name__ == '__main__': dy_order = DYOrder(1) # type 1 豎向條形圖 2 橫向 dy_order.to_run(2)
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python爬取豆瓣電影排行榜(requests)的示例代碼
- python使用re模塊爬取豆瓣Top250電影
- 用Python 爬取貓眼電影數(shù)據(jù)分析《無(wú)名之輩》
- python實(shí)現(xiàn)的爬取電影下載鏈接功能示例
- Python爬蟲(chóng)——爬取豆瓣電影Top250代碼實(shí)例
- python使用BeautifulSoup與正則表達(dá)式爬取時(shí)光網(wǎng)不同地區(qū)top100電影并對(duì)比
- python使用requests模塊實(shí)現(xiàn)爬取電影天堂最新電影信息
- Python趣味爬蟲(chóng)之爬取愛(ài)奇藝熱門(mén)電影
相關(guān)文章
對(duì)Python 獲取類的成員變量及臨時(shí)變量的方法詳解
今天小編就為大家分享一篇對(duì)Python 獲取類的成員變量及臨時(shí)變量的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Python中的數(shù)學(xué)運(yùn)算操作符使用進(jìn)階
這篇文章主要介紹了Python中的數(shù)學(xué)運(yùn)算操作符使用進(jìn)階,也包括運(yùn)算賦值操作符等基本知識(shí)的小結(jié),需要的朋友可以參考下2016-06-06Python Numpy中數(shù)據(jù)的常用保存與讀取方法
這篇文章主要介紹了Python Numpy中數(shù)據(jù)的常用保存與讀取方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04使用pandas對(duì)兩個(gè)dataframe進(jìn)行join的實(shí)例
今天小編就為大家分享一篇使用pandas對(duì)兩個(gè)dataframe進(jìn)行join的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Python實(shí)現(xiàn)PS圖像調(diào)整黑白效果示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS圖像調(diào)整黑白效果,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)PS圖像的黑白效果原理與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Python 內(nèi)置函數(shù)complex詳解
這篇文章主要介紹了Python 內(nèi)置函數(shù)complex詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10