本文首發(fā)于公眾號(hào):Python編程與實(shí)戰(zhàn)
最近,朋友圈和微博被動(dòng)畫《哪吒之魔童降世》刷屏了。
對(duì)哪吒的記憶還停留在小時(shí)候看的動(dòng)畫片,是他,是他,就是他,我們的小朋友小哪吒。

穿個(gè)紅色肚兜,扎兩個(gè)小辮子,讓小時(shí)候的我一度懷疑這是男是女???
然后我看到這部片子的宣傳海報(bào),這尼瑪這是什么妖魔?

直到我走出電影院之后
啪啪啪打臉,真香。
電影上映之后,無論是票房還是口碑一路炸裂

上映 14 天,累計(jì)票房 31.9 億,在中國電影票房史上第 8 名,不出意外能入進(jìn)前五名

為了能讓大家有個(gè)更加直觀的感受,所以我用 Python 爬取分析了電影相關(guān)的數(shù)據(jù)

數(shù)據(jù)抓取
主要抓取的是電影從上映到今天的所有票房數(shù)據(jù),以及和其它同期上映的電影一些對(duì)比情況

數(shù)據(jù)來源地址:http://piaofang.baidu.com/
老規(guī)矩,人狠話不多,直接貼代碼了
@classmethod
def spider(cls):
cls.session.get("https://piaofang.baidu.com/?sfrom=wise_film_box")
lz_list = []
szw_list = []
for r in [datetime.now() - timedelta(days=i) for i in range(0, 14)]:
params = {
"pagelets[]": "index-overall",
"reqID": "28",
"sfrom": "wise_film_box",
"date": r.strftime("%Y-%m-%d"),
"attr": "3,4,5,6",
"t": int(time.time() * 1000),
}
response = cls.session.get("https://piaofang.baidu.com/", params=params).text
result = eval(re.findall("BigPipe.onPageletArrive\((.*?)\)", response)[0])
selector = Selector(text=result.get("html"))
li_list = selector.css(".detail-list .list dd")
for d in range(len(li_list)):
dic = {}
name = li_list[d].css("h3 b ::text").extract_first()
if '哪吒' in name or "烈火" in name:
total_box = li_list[d].css("h3 span ::attr(data-box-office)").extract_first() # 總票房
box = li_list[d].css("div span[data-index='3'] ::text").extract_first() # 實(shí)時(shí)票房
ratio = li_list[d].css("div span[data-index='4'] ::text").extract_first() # 票房占比
movie_ratio = li_list[d].css("div span[data-index='5'] ::text").extract_first() # 排片占比
dic["date"] = r.strftime("%Y-%m-%d")
dic["total_box"] = float(
total_box.replace("億", "")) * 10000 if "億" in total_box else total_box.replace("萬", "")
dic["box"] = float(box.replace("億", "")) * 10000 if "億" in box else box.replace("萬", "")
dic["ratio"] = ratio
dic["movie_ratio"] = movie_ratio
lz_list.append(dic) if '哪吒' in name else szw_list.append(dic)
return lz_list, szw_list
這是 class 類方法,因?yàn)橛玫搅祟愖兞?,所以上面有個(gè)裝飾器。你也可以寫成普通方法,看個(gè)人習(xí)慣...
上面的代碼將 《哪吒之魔童降世》和《烈火英雄》相關(guān)數(shù)據(jù)都爬下來了
數(shù)據(jù)可視化
主要基于 pyecharts 模塊,入門教程相關(guān)文章在此,用到的方法,這里面基本都講過
上映到今天的每日票房,基于 Bar 模塊

總票房走勢(shì)圖

看這票房走勢(shì),再加上周末兩天,40 億不是夢(mèng)
部分代碼如下:
@staticmethod
def line_base(l1, l2) -> Line:
lh_list = [y["total_box"] for y in l2]
lh_list.extend([0 for _ in range(3)]) # 前面三天為0
c = (
Line(init_opts=opts.InitOpts(bg_color="", page_title="總票房"))
.add_xaxis([y["date"] for y in reversed(l1)])
.add_yaxis("哪吒之魔童降世", [y["total_box"] for y in reversed(l1)], is_smooth=True, markpoint_opts=opts.
MarkPointOpts(data=[opts.MarkPointItem(type_="max")]))
.add_yaxis("烈火英雄", reversed(lh_list), is_smooth=True, markpoint_opts=opts.
MarkPointOpts(data=[opts.MarkPointItem(type_="max")]))
.set_global_opts(title_opts=opts.TitleOpts(title="總票房", subtitle_textstyle_opts={"color": "red"},
subtitle="單位: 萬元"), toolbox_opts=opts.ToolboxOpts())
)
return c.render("line.html")
再看下排片情況

嗯哼,嘗起來像甜甜圈,某籃球巨星如是說到..
那么票房占比呢?

排片只有 38%,票房卻占了 半壁江山
哪吒就是這么強(qiáng) !后臺(tái)回復(fù) “1024” 獲得代碼

注:以上數(shù)據(jù)截至2019/08/08