Python数据分析告诉你哪吒到底有多火!
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python数据分析告诉你哪吒到底有多火!,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3486字,纯文字阅读大概需要5分钟。
内容图文
![Python数据分析告诉你哪吒到底有多火!](/upload/InfoBanner/zyjiaocheng/737/886072e3a1eb4e439bbe4183fcffe3d1.jpg)
最近,朋友圈和微博被动画《哪吒之魔童降世》刷屏了。
对哪吒的记忆还停留在小时候看的动画片:是他,是他,就是他,我们的小朋友小哪吒。
上映 14 天,累计票房 31.9 亿,在中国电影票房史上排在第 8 名,不出意外最终能进入排行榜前五名
为了能让大家有个更加直观的感受,所以我用 Python 爬取并分析了电影相关的数据
数据来源地址:http://piaofang.baidu.com/
贴代码了
@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 模块来做数据可视化
总票房走势图
看这票房走势,再加上周末两天,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%,票房却占了 半壁江山
哪吒就是这么强 !
文章来源于公总号黑马程序员广州中心(itheimagz)更多资源请关注
内容总结
以上是互联网集市为您收集整理的Python数据分析告诉你哪吒到底有多火!全部内容,希望文章能够帮你解决Python数据分析告诉你哪吒到底有多火!所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。