1. 程式人生 > 實用技巧 >python資料分析告訴你,哪吒到底有多火?

python資料分析告訴你,哪吒到底有多火?

本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯絡我們以作處理

以下文章來源於騰訊雲 作者:Python程式設計與實戰

( 想要學習Python?Python學習交流群:1039649593,滿足你的需求,資料都已經上傳群檔案流,可以自行下載!還有海量最新2020python學習資料。 )

最近,朋友圈和微博被動畫《哪吒之魔童降世》刷屏了。
對哪吒的記憶還停留在小時候看的動畫片:是他,是他,就是他,我們的小朋友小哪吒。

穿個紅色肚兜,扎兩個小辮子,讓小時候的我一度懷疑這是男是女???
然後我看到這部片子的宣傳海報,這尼瑪這是什麼妖魔?


直到我走出電影院之後
啪啪啪打臉,真香。

電影上映之後,無論是票房還是口碑一路炸裂

上映 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%,票房卻佔了 半壁江山