Python數據可視化:2018年電影分析

有態度地學習

雙11已經過去,雙12即將來臨,離2018年的結束也就2個月不到,還記得年初立下的flag嗎?

完成了多少?相信很多人和我一樣,抱頭痛哭...

本次利用貓眼電影,實現對2018年的電影大數據進行分析。

/ 01 / 網頁分析

01 標籤

通過點擊貓眼電影已經歸類好的標籤,得到網址信息。

02 索引頁

打開開發人員工具,獲取索引頁裏電影的鏈接以及評分信息。

索引頁一共有30多頁,但是有電影評分的只有10頁。

本次只對有電影評分的數據進行獲取。

03 詳情頁

對詳情頁的信息進行獲取。

主要是名稱,類型,國家,時長,上映時間,評分,評分人數,累計票房。

/ 02 / 反爬破解

通過開發人員工具發現,貓眼針對評分,評分人數,累計票房的數據,施加了文字反爬。

通過查看網頁源碼,發現只要刷新頁面,三處文字編碼就會改變,無法直接匹配信息。

所以需要下載文字文件,對其進行雙匹配。

from fontTools.ttLib import TTFont

#font = TTFont('base.woff')
#font.saveXML('base.xml')
font = TTFont('maoyan.woff')
font.saveXML('maoyan.xml')

將woff格式轉換爲xml格式,以便在Pycharm中查看詳細信息。

利用下面這個網站,打開woff文件。

url: http://fontstore.baidu.com/static/editor/index.html

可以得到下面數字部分信息(上下兩塊)。

在Pycharm中查看xml格式文件(左右兩塊),你就會發現有對應信息。

通過上圖你就可以將數字6對上號了,其他數字一樣的。

def get_numbers(u):
    """
    對貓眼的文字反爬進行破解
    """
    cmp = re.compile(",\n           url\('(//.*.woff)'\) format\('woff'\)")
    rst = cmp.findall(u)
    ttf = requests.get("http:" + rst[0], stream=True)
    with open("maoyan.woff", "wb") as pdf:
        for chunk in ttf.iter_content(chunk_size=1024):
            if chunk:
                pdf.write(chunk)
    base_font = TTFont('base.woff')
    maoyanFont = TTFont('maoyan.woff')
    maoyan_unicode_list = maoyanFont['cmap'].tables[0].ttFont.getGlyphOrder()
    maoyan_num_list = []
    base_num_list = ['.', '3', '0', '8', '9', '4', '1', '5', '2', '7', '6']
    base_unicode_list = ['x', 'uniF561', 'uniE6E1', 'uniF125', 'uniF83F', 'uniE9E2', 'uniEEA6', 'uniEEC2', 'uniED38', 'uniE538', 'uniF8E7']
    for i in range(1, 12):
        maoyan_glyph = maoyanFont['glyf'][maoyan_unicode_list[i]]
        for j in range(11):
            base_glyph = base_font['glyf'][base_unicode_list[j]]
            if maoyan_glyph == base_glyph:
                maoyan_num_list.append(base_num_list[j])
                break
    maoyan_unicode_list[1] = 'uni0078'
    utf8List = [eval(r"'\u" + uni[3:] + "'").encode("utf-8") for uni in maoyan_unicode_list[1:]]
    utf8last = []
    for i in range(len(utf8List)):
        utf8List[i] = str(utf8List[i], encoding='utf-8')
        utf8last.append(utf8List[i])
    return (maoyan_num_list ,utf8last)

/ 03 / 數據獲取

01 構造請求頭

head = """
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:maoyan.com
Upgrade-Insecure-Requests:1
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36
"""

def str_to_dict(header):
    """
    構造請求頭,可以在不同函數裏構造不同的請求頭
    """
    header_dict = {}
    header = header.split('\n')
    for h in header:
        h = h.strip()
        if h:
            k, v = h.split(':', 1)
            header_dict[k] = v.strip()
    return header_dict

因爲索引頁和詳情頁請求頭不一樣,這裏爲了簡便,構造了一個函數。

02 獲取電影詳情頁鏈接

def get_url():
    """
    獲取電影詳情頁鏈接
    """
    for i in range(0, 300, 30):
        time.sleep(10)
        url = 'http://maoyan.com/films?showType=3&yearId=13&sortId=3&offset=' + str(i)
        host = """Referer:http://maoyan.com/films?showType=3&yearId=13&sortId=3&offset=0
        """
        header = head + host
        headers = str_to_dict(header)
        response = requests.get(url=url, headers=headers)
        html = response.text
        soup = BeautifulSoup(html, 'html.parser')
        data_1 = soup.find_all('div', {'class': 'channel-detail movie-item-title'})
        data_2 = soup.find_all('div', {'class': 'channel-detail channel-detail-orange'})
        num = 0
        for item in data_1:
            num += 1
            time.sleep(10)
            url_1 = item.select('a')[0]['href']
            if data_2[num-1].get_text() != '暫無評分':
                url = 'http://maoyan.com' + url_1
                for message in get_message(url):
                    print(message)
                    to_mysql(message)
                print(url)
                print('---------------^^^Film_Message^^^-----------------')
            else:
                print('The Work Is Done')
                break

03 獲取電影詳情頁信息

def get_message(url):
    """
    獲取電影詳情頁裏的信息
    """
    time.sleep(10)
    data = {}
    host = """refer: http://maoyan.com/news
    """
    header = head + host
    headers = str_to_dict(header)
    response = requests.get(url=url, headers=headers)
    u = response.text
    # 破解貓眼文字反爬
    (mao_num_list, utf8last) = get_numbers(u)
    # 獲取電影信息
    soup = BeautifulSoup(u, "html.parser")
    mw = soup.find_all('span', {'class': 'stonefont'})
    score = soup.find_all('span', {'class': 'score-num'})
    unit = soup.find_all('span', {'class': 'unit'})
    ell = soup.find_all('li', {'class': 'ellipsis'})
    name = soup.find_all('h3', {'class': 'name'})
    # 返回電影信息
    data["name"] = name[0].get_text()
    data["type"] = ell[0].get_text()
    data["country"] = ell[1].get_text().split('/')[0].strip().replace('\n', '')
    data["length"] = ell[1].get_text().split('/')[1].strip().replace('\n', '')
    data["released"] = ell[2].get_text()[:10]
    # 因爲會出現沒有票房的電影,所以這裏需要判斷
    if unit:
        bom = ['分', score[0].get_text().replace('.', '').replace('萬', ''), unit[0].get_text()]
        for i in range(len(mw)):
            moviewish = mw[i].get_text().encode('utf-8')
            moviewish = str(moviewish, encoding='utf-8')
            # 通過比對獲取反爬文字信息
            for j in range(len(utf8last)):
                moviewish = moviewish.replace(utf8last[j], maoyan_num_list[j])
            if i == 0:
                data["score"] = moviewish + bom[i]
            elif i == 1:
                if '萬' in moviewish:
                    data["people"] = int(float(moviewish.replace('萬', '')) * 10000)
                else:
                    data["people"] = int(float(moviewish))
            else:
                if '萬' == bom[i]:
                    data["box_office"] = int(float(moviewish) * 10000)
                else:
                    data["box_office"] = int(float(moviewish) * 100000000)
    else:
        bom = ['分', score[0].get_text().replace('.', '').replace('萬', ''), 0]
        for i in range(len(mw)):
            moviewish = mw[i].get_text().encode('utf-8')
            moviewish = str(moviewish, encoding='utf-8')
            for j in range(len(utf8last)):
                moviewish = moviewish.replace(utf8last[j], maoyan_num_list[j])
            if i == 0:
                data["score"] = moviewish + bom[i]
            else:
                if '萬' in moviewish:
                    data["people"] = int(float(moviewish.replace('萬', '')) * 10000)
                else:
                    data["people"] = int(float(moviewish))
        data["box_office"] = bom[2]
    yield data

/ 04 / 數據存儲

01 創建數據庫及表格

db = pymysql.connect(host='127.0.0.1', user='root', password='774110919', port=3306)
cursor = db.cursor()
cursor.execute("CREATE DATABASE maoyan DEFAULT CHARACTER SET utf8mb4")
db.close()
db = pymysql.connect(host='127.0.0.1', user='root', password='774110919', port=3306, db='maoyan')
cursor = db.cursor()
sql = 'CREATE TABLE IF NOT EXISTS films (name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, country VARCHAR(255) NOT NULL, length VARCHAR(255) NOT NULL, released VARCHAR(255) NOT NULL, score VARCHAR(255) NOT NULL, people INT NOT NULL, box_office BIGINT NOT NULL, PRIMARY KEY (name))'
cursor.execute(sql)
db.close()

其中票房收入數據類型爲BIGINT(19位數),最大爲18446744073709551615。

INT(10位數),最大爲2147483647,達不到36億(3600000000)。

02 數據存儲

def to_mysql(data):
    """
    信息寫入mysql
    """
    table = 'films'
    keys = ', '.join(data.keys())
    values = ', '.join(['%s'] * len(data))
    db = pymysql.connect(host='localhost', user='root', password='774110919', port=3306, db='maoyan')
    cursor = db.cursor()
    sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=table, keys=keys, values=values)
    try:
        if cursor.execute(sql, tuple(data.values())):
            print("Successful")
            db.commit()
    except:
        print('Failed')
        db.rollback()
    db.close()

最後成功存儲數據

/ 05 / 數據可視化

可視化源碼就不放了,公衆號回覆電影即可獲得。

01 電影票房TOP10

還剩一個多月,不知道榜單上會不會有新成員。最近「毒液」很火,蠻有希望。

02 電影評分TOP10

這裏就得吐槽一下pyecharts,座標轉換後,座標值名稱太長就會被遮擋,還需改進呢~

03 電影人氣TOP10

茫茫人海之中,相信一定也有大家的身影,我也是其中的一員!!!

04 每月電影上映數量

每月上映數好像沒什麼大差距,7月最少,難道是因爲天氣熱?

05 每月電影票房

這裏就看出春節檔電影的威力了,金三銀四、金九銀十,各行各業的規律,電影行業也不例外。

上一張圖我們知道7月份電影上新最少,票房反而是第二。

這裏看了下數據,發現有「我不是藥神」「西虹市首富」「邪不壓正」「摩天營救」「狄仁傑之四大天王」幾部大劇撐着。

06 各國家電影數量TOP10

原來中國電影這麼高產的,可是豆瓣TOP250裏又有多少中國電影呢?深思!!!

07 中外票房對比

2017年的年度票房是560億,估計今年快要突破了。據說今年全年票房有望突破600億。

08 電影名利雙收TOP10

計算公式是,把某部電影的評分在所有電影評分中的排名與這部電影的票房在所有票房中的排名加起來,再除以電影總數。

除了「侏羅紀世界2」「無雙」「捉妖記2」,我都看過啦!

09 電影叫座不叫好TOP10

計算公式是,把某部電影的票房排名減去某部電影的評分排名加起來,再除以電影總數。

可能是貓眼的用戶比較仁慈吧,與豆瓣相比,普遍評分都比較高。我個人都不太敢相信這個結果。

不過有一個還是挺準的,「愛情公寓」。

10 電影類型分佈

劇情電影永遠引人深思。感覺今年的電影好多跟錢有關,比如「我不是藥神」「西虹市首富」「一出好戲」「頭號玩家」,貧窮限制了大傢伙們。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章