In this article, the example of Python crawler crawling movie box office data and chart display operation. Shared for your reference, as follows:
Crawler Movie All-Time Box Office Rankings/BoxOffice/getInland?pIndex=1&t=0
- Python crawls historical movie box office records
- Parsing Json Data
- Horizontal bar graph presentation
- object-oriented thinking
Importing related libraries
import requests import re from matplotlib import pyplot as plt from matplotlib import font_manager import json
Class Code Section
class DYOrder(object): # Initialization def __init__(self,page=1): = '/BoxOffice/getInland?pIndex={}&t=0'.format(page) = {'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'} # Request def __to_request(self): response = (url=,headers=) return self.__to_parse(('utf-8')) # parsing def __to_parse(self,html): # Returns a JSON string # First deserialize the string into a JSON object my_json = (html) return my_json #Charting def __to_show(self,data,show_type): x = [] y = [] for value in data: (value['MovieName']) (int(value['BoxOffice'])) my_font = font_manager.FontProperties(fname='/System/Library/Fonts/',size=18) if show_type == 1: (figsize=(20,8),dpi=80) rects = (range(len(x)),[float(i) for i in y],width=0.5,color='red') (range(len(x)),x,fontproperties=my_font,rotation=60) ('Name',rotation=60,color='blue',fontproperties=my_font) ('Box office/million',rotation=60,color='blue',fontproperties=my_font) for rect in rects: height = rect.get_height() (rect.get_x() + rect.get_width()/2,height+0.4,str(height),ha='center',rotation=30) else: # Horizontal (y,x) (figsize=(15,13),dpi=80) rects = (range(len(x)),y,height=0.8,color='orange') (range(len(x)),x,fontproperties=my_font,rotation=30) ('Name',rotation=0,color='blue',fontproperties=my_font) ('Box office/million',rotation=60,color='blue',fontproperties=my_font) for rect in rects: width = rect.get_width() (width, rect.get_y()+0.3/2,str(width),va='center',rotation=30) (alpha=0.4) ('Historical Box Office Ranking of Chinese Movies',color='red',size=18,fontproperties=my_font) () # All operations def to_run(self,show_type=1): result = self.__to_request() self.__to_show(result,show_type)
Call the class and show
if __name__ == '__main__': dy_order = DYOrder(1) # type 1 Vertical bar graph 2 Horizontal dy_order.to_run(2)
More about Python related content can be viewed on this site topic: thePython Socket Programming Tips Summary》、《Python Regular Expression Usage Summary》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
I hope that what I have said in this article will help you in Python programming.