《哪吒之魔童降世》席卷全球!用Python数据分析告诉你为什么这么火

最近,朋友圈和微博被动画《哪吒之魔童降世》刷屏了。 对哪吒的记忆还停留在小时候看的动画片,是他,是他,就是他,我们的小朋友小哪吒。

穿个红色肚兜,扎两个小辫子,让小时候的我一度怀疑这是男是女??? 然后我看到这部片子的宣传海报,这尼玛这是什么妖魔?

直到我走出电影院之后 啪啪啪打脸,真香。 电影上映之后,无论是票房还是口碑一路炸裂

上映 14 天,累计票房 31.9 亿,在中国电影票房史上第 8 名,不出意外能入进前五名

为了能让大家有个更加直观的感受,所以我用 Python 爬取分析了电影相关的数据

数据抓取

主要抓取的是电影从上映到今天的所有票房数据,以及和其它同期上映的电影一些对比情况

数据来源地址:piaofang.baidu.com/ 老规矩,人狠话不多,直接贴代码了


在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
@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()  # 实时票房
				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 类方法,因为用到了类变量,所以上面有个装饰器。你也可以写成普通方法,看个人习惯… 上面的代码将 《哪吒之魔童降世》和《烈火英雄》相关数据都爬下来了

数据可视化

主要基于 pyecharts 模块,入门教程相关文章在此,用到的方法,这里面基本都讲过 上映到今天的每日票房,基于 Bar 模块

下图是哪吒和烈火英雄,上映之后的每日票房数据,可以看到 08/07 这天数据额外的高,原来是七夕…

[

总票房走势图

看这票房走势,再加上周末两天,40 亿不是梦

部分代码如下:

@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%,票房却占了 半壁江山 哪吒就是这么强 !

如果你依然在编程的世界里迷茫,可以加入我们的Python学习扣qun:784758214,看看前辈们是如何学习的!交流经验!自己是一名高级python开发工程师,从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

注:以上数据截至2019/08/08

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值