前序
过完新年,电影《你好,李焕英》口碑不错,票房至今还在趋势增长中,下面通过程序方法,获取票房增长曲线。
数据获取
数据来源:猫眼
数据接口:
http://piaofang.maoyan.com/dashboard-ajax/movie?orderType=0&showDate=
部分代码如下:
def get_box_data(self, days):
des_date = self.get_date(days=days)
url = self.box_rate_url + des_date
box_dict = requests.get(url, headers=self.headers).json()
return des_date, box_dict
代码实现
- 票房走势:这里拿《你好,李焕英》和《唐探3》两部电影做比较
def del_box_rate(self, box_dict):
movie = []
if "list" in box_dict["movieList"].keys():
box_list = box_dict["movieList"]["list"]
for _box in box_list:
if _box["movieInfo"]["movieId"] == self.movieid:
movie.append(_box["sumBoxDesc"][:-1])
return movie
@staticmethod
def get_date(days=0):
return (datetime.datetime.now() - datetime.timedelta(days=days)).strftime("%Y%m%d")
def date_box_list(self):
date_list = []
movie_list = []
# 倒着取值, 左闭右开,步长
for period in range(7, -1, -1):
des_date, box_dict = self.get_box_data(days=period)
movie = self.del_box_rate(box_dict)
date_list.append(des_date)
movie_list.extend(movie)
return date_list, movie_list
def box_thread(self):
date_list = []
s1299372 = list()
s1217023 = list()
for k, v in self.movie_dict.items():
if k == 1299372:
self.movieid = 1299372
date_list, s1299372 = self.date_box_list()
elif k == 1217023:
self.movieid = 1217023
date_list, s1217023 = self.date_box_list()
line = (
Line()
.add_xaxis(date_list)
.add_yaxis("你好,李焕英", s1299372, color="blue")
.add_yaxis("唐人街探案3", s1217023, color="black")
.set_global_opts(
title_opts=opts.TitleOpts(title="票房走势图", title_textstyle_opts=opts.TextStyleOpts(font_size=14)),
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value}/亿"),
)
)
)
line.render("票房走势图.html")
- 某天票房站比率:以7部春节档做参考数据。
def show_count(self):
data = []
graph = Graph()
for k, v in self.movie_dict.items():
self.movieid = k
des_date, box_dict = self.get_box_data(days=0)
movie = self.del_show_count(box_dict)
data.append((v, movie))
graph.json_data = data
print(graph.json_data)
graph.m_pie()
效果图
票房走势图
image.png
某天票房占比率:提取七部电影某天的票房占比,绘成饼图格式
image.png
不难发现,《你好,李焕英》票房还是占大头的,其次是《唐探3》。
看来我得找个时间去看下李焕英这部电影。