偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python數(shù)據(jù)可視化:2018年電影分析

大數(shù)據(jù) 數(shù)據(jù)可視化 數(shù)據(jù)分析
雙11已經(jīng)過去,雙12即將來(lái)臨,離2018年的結(jié)束也就2個(gè)月不到,還記得年初立下的flag嗎?完成了多少?相信很多人和我一樣,抱頭痛哭...本次利用貓眼電影,實(shí)現(xiàn)對(duì)2018年的電影大數(shù)據(jù)進(jìn)行分析。

雙11已經(jīng)過去,雙12即將來(lái)臨,離2018年的結(jié)束也就2個(gè)月不到,還記得年初立下的flag嗎?

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

本次利用貓眼電影,實(shí)現(xiàn)對(duì)2018年的電影大數(shù)據(jù)進(jìn)行分析。

Python數(shù)據(jù)可視化:2018年電影分析

1 網(wǎng)頁(yè)分析

01 標(biāo)簽

Python數(shù)據(jù)可視化:2018年電影分析

通過點(diǎn)擊貓眼電影已經(jīng)歸類好的標(biāo)簽,得到網(wǎng)址信息。

02 索引頁(yè)

Python數(shù)據(jù)可視化:2018年電影分析

打開開發(fā)人員工具,獲取索引頁(yè)里電影的鏈接以及評(píng)分信息。

索引頁(yè)一共有30多頁(yè),但是有電影評(píng)分的只有10頁(yè)。

本次只對(duì)有電影評(píng)分的數(shù)據(jù)進(jìn)行獲取。

03 詳情頁(yè)

Python數(shù)據(jù)可視化:2018年電影分析

對(duì)詳情頁(yè)的信息進(jìn)行獲取。

主要是名稱,類型,國(guó)家,時(shí)長(zhǎng),上映時(shí)間,評(píng)分,評(píng)分人數(shù),累計(jì)票房。

2 反爬破解

Python數(shù)據(jù)可視化:2018年電影分析

通過開發(fā)人員工具發(fā)現(xiàn),貓眼針對(duì)評(píng)分,評(píng)分人數(shù),累計(jì)票房的數(shù)據(jù),施加了文字反爬。

Python數(shù)據(jù)可視化:2018年電影分析

通過查看網(wǎng)頁(yè)源碼,發(fā)現(xiàn)只要刷新頁(yè)面,三處文字編碼就會(huì)改變,無(wú)法直接匹配信息。

所以需要下載文字文件,對(duì)其進(jìn)行雙匹配。

  1. from fontTools.ttLib import TTFont 
  2.  
  3. #font = TTFont('base.woff'
  4. #font.saveXML('base.xml'
  5. font = TTFont('maoyan.woff'
  6. font.saveXML('maoyan.xml'

將woff格式轉(zhuǎn)換為xml格式,以便在Pycharm中查看詳細(xì)信息。

利用下面這個(gè)網(wǎng)站,打開woff文件。

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

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

在Pycharm中查看xml格式文件(左右兩塊),你就會(huì)發(fā)現(xiàn)有對(duì)應(yīng)信息。

Python數(shù)據(jù)可視化:2018年電影分析

通過上圖你就可以將數(shù)字6對(duì)上號(hào)了,其他數(shù)字一樣的。

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

3 數(shù)據(jù)獲取

01 構(gòu)造請(qǐng)求頭

  1. head = ""
  2. Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 
  3. Accept-Encoding:gzip, deflate, br 
  4. Accept-Language:zh-CN,zh;q=0.8 
  5. Cache-Control:max-age=0 
  6. Connection:keep-alive 
  7. Host:maoyan.com 
  8. Upgrade-Insecure-Requests:1 
  9. Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
  10. User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36 
  11. ""
  12.  
  13. def str_to_dict(header): 
  14.     ""
  15.     構(gòu)造請(qǐng)求頭,可以在不同函數(shù)里構(gòu)造不同的請(qǐng)求頭 
  16.     ""
  17.     header_dict = {} 
  18.     header = header.split('\n'
  19.     for h in header: 
  20.         h = h.strip() 
  21.         if h: 
  22.             k, v = h.split(':', 1) 
  23.             header_dict[k] = v.strip() 
  24.     return header_dict 

因?yàn)樗饕?yè)和詳情頁(yè)請(qǐng)求頭不一樣,這里為了簡(jiǎn)便,構(gòu)造了一個(gè)函數(shù)。

02 獲取電影詳情頁(yè)鏈接

  1. def get_url(): 
  2.     ""
  3.     獲取電影詳情頁(yè)鏈接 
  4.     ""
  5.     for i in range(0, 300, 30): 
  6.         time.sleep(10) 
  7.         url = 'http://maoyan.com/films?showType=3&yearId=13&sortId=3&offset=' + str(i) 
  8.         host = """Referer:http://maoyan.com/films?showType=3&yearId=13&sortId=3&offset=0 
  9.         ""
  10.         header = head + host 
  11.         headers = str_to_dict(header) 
  12.         response = requests.get(url=url, headers=headers) 
  13.         html = response.text 
  14.         soup = BeautifulSoup(html, 'html.parser'
  15.         data_1 = soup.find_all('div', {'class''channel-detail movie-item-title'}) 
  16.         data_2 = soup.find_all('div', {'class''channel-detail channel-detail-orange'}) 
  17.         num = 0 
  18.         for item in data_1: 
  19.             num += 1 
  20.             time.sleep(10) 
  21.             url_1 = item.select('a')[0]['href'
  22.             if data_2[num-1].get_text() != '暫無(wú)評(píng)分'
  23.                 url = 'http://maoyan.com' + url_1 
  24.                 for message in get_message(url): 
  25.                     print(message) 
  26.                     to_mysql(message) 
  27.                 print(url) 
  28.                 print('---------------^^^Film_Message^^^-----------------'
  29.             else
  30.                 print('The Work Is Done'
  31.                 break 

03 獲取電影詳情頁(yè)信息

  1. def get_message(url): 
  2.     ""
  3.     獲取電影詳情頁(yè)里的信息 
  4.     ""
  5.     time.sleep(10) 
  6.     data = {} 
  7.     host = """refer: http://maoyan.com/news 
  8.     ""
  9.     header = head + host 
  10.     headers = str_to_dict(header) 
  11.     response = requests.get(url=url, headers=headers) 
  12.     u = response.text 
  13.     # 破解貓眼文字反爬 
  14.     (mao_num_list, utf8last) = get_numbers(u) 
  15.     # 獲取電影信息 
  16.     soup = BeautifulSoup(u, "html.parser"
  17.     mw = soup.find_all('span', {'class''stonefont'}) 
  18.     score = soup.find_all('span', {'class''score-num'}) 
  19.     unit = soup.find_all('span', {'class''unit'}) 
  20.     ell = soup.find_all('li', {'class''ellipsis'}) 
  21.     name = soup.find_all('h3', {'class''name'}) 
  22.     # 返回電影信息 
  23.     data["name"] = name[0].get_text() 
  24.     data["type"] = ell[0].get_text() 
  25.     data["country"] = ell[1].get_text().split('/')[0].strip().replace('\n'''
  26.     data["length"] = ell[1].get_text().split('/')[1].strip().replace('\n'''
  27.     data["released"] = ell[2].get_text()[:10] 
  28.     # 因?yàn)闀?huì)出現(xiàn)沒有票房的電影,所以這里需要判斷 
  29.     if unit: 
  30.         bom = ['分', score[0].get_text().replace('.''').replace('萬(wàn)'''), unit[0].get_text()] 
  31.         for i in range(len(mw)): 
  32.             moviewish = mw[i].get_text().encode('utf-8'
  33.             moviewish = str(moviewish, encoding='utf-8'
  34.             # 通過比對(duì)獲取反爬文字信息 
  35.             for j in range(len(utf8last)): 
  36.                 moviewish = moviewish.replace(utf8last[j], maoyan_num_list[j]) 
  37.             if i == 0: 
  38.                 data["score"] = moviewish + bom[i] 
  39.             elif i == 1: 
  40.                 if '萬(wàn)' in moviewish: 
  41.                     data["people"] = int(float(moviewish.replace('萬(wàn)''')) * 10000) 
  42.                 else
  43.                     data["people"] = int(float(moviewish)) 
  44.             else
  45.                 if '萬(wàn)' == bom[i]: 
  46.                     data["box_office"] = int(float(moviewish) * 10000) 
  47.                 else
  48.                     data["box_office"] = int(float(moviewish) * 100000000) 
  49.     else
  50.         bom = ['分', score[0].get_text().replace('.''').replace('萬(wàn)'''), 0] 
  51.         for i in range(len(mw)): 
  52.             moviewish = mw[i].get_text().encode('utf-8'
  53.             moviewish = str(moviewish, encoding='utf-8'
  54.             for j in range(len(utf8last)): 
  55.                 moviewish = moviewish.replace(utf8last[j], maoyan_num_list[j]) 
  56.             if i == 0: 
  57.                 data["score"] = moviewish + bom[i] 
  58.             else
  59.                 if '萬(wàn)' in moviewish: 
  60.                     data["people"] = int(float(moviewish.replace('萬(wàn)''')) * 10000) 
  61.                 else
  62.                     data["people"] = int(float(moviewish)) 
  63.         data["box_office"] = bom[2] 
  64.     yield data 

4 數(shù)據(jù)存儲(chǔ)

01 創(chuàng)建數(shù)據(jù)庫(kù)及表格

 

  1. db = pymysql.connect(host='127.0.0.1'user='root'password='774110919', port=3306) 
  2. cursor = db.cursor() 
  3. cursor.execute("CREATE DATABASE maoyan DEFAULT CHARACTER SET utf8mb4"
  4. db.close() 
  5.  
  6. db = pymysql.connect(host='127.0.0.1'user='root'password='774110919', port=3306, db='maoyan'
  7. cursor = db.cursor() 
  8. 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))' 
  9. cursor.execute(sql) 
  10. db.close() 

其中票房收入數(shù)據(jù)類型為BIGINT(19位數(shù)),***為18446744073709551615。

INT(10位數(shù)),***為2147483647,達(dá)不到36億(3600000000)。

02 數(shù)據(jù)存儲(chǔ)

  1. def to_mysql(data): 
  2.     ""
  3.     信息寫入mysql 
  4.     ""
  5.     table = 'films' 
  6.     keys = ', '.join(data.keys()) 
  7.     values = ', '.join(['%s'] * len(data)) 
  8.     db = pymysql.connect(host='localhost'user='root'password='774110919', port=3306, db='maoyan'
  9.     cursor = db.cursor() 
  10.     sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=table, keys=keys, values=values
  11.     try: 
  12.         if cursor.execute(sql, tuple(data.values())): 
  13.             print("Successful"
  14.             db.commit() 
  15.     except
  16.         print('Failed'
  17.         db.rollback() 
  18.     db.close() 

***成功存儲(chǔ)數(shù)據(jù)

Python數(shù)據(jù)可視化:2018年電影分析

5 數(shù)據(jù)可視化

可視化源碼就不放了,公眾號(hào)回復(fù)電影即可獲得。

01 電影票房***0

Python數(shù)據(jù)可視化:2018年電影分析

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

02 電影評(píng)分***0

Python數(shù)據(jù)可視化:2018年電影分析

這里就得吐槽一下pyecharts,坐標(biāo)轉(zhuǎn)換后,坐標(biāo)值名稱太長(zhǎng)就會(huì)被遮擋,還需改進(jìn)呢~

03 電影人氣***0

Python數(shù)據(jù)可視化:2018年電影分析

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

04 每月電影上映數(shù)量

Python數(shù)據(jù)可視化:2018年電影分析

每月上映數(shù)好像沒什么大差距,7月最少,難道是因?yàn)樘鞖鉄?

05 每月電影票房

Python數(shù)據(jù)可視化:2018年電影分析

這里就看出春節(jié)檔電影的威力了,金三銀四、金九銀十,各行各業(yè)的規(guī)律,電影行業(yè)也不例外。

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

這里看了下數(shù)據(jù),發(fā)現(xiàn)有「我不是藥神」「西虹市首富」「邪不壓正」「摩天營(yíng)救」「狄仁杰之四大天王」幾部大劇撐著。

06 各國(guó)家電影數(shù)量***0

Python數(shù)據(jù)可視化:2018年電影分析

原來(lái)中國(guó)電影這么高產(chǎn)的,可是豆瓣TOP250里又有多少中國(guó)電影呢?深思!!!

07 中外票房對(duì)比

Python數(shù)據(jù)可視化:2018年電影分析

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

08 電影名利雙收***0

Python數(shù)據(jù)可視化:2018年電影分析

計(jì)算公式是,把某部電影的評(píng)分在所有電影評(píng)分中的排名與這部電影的票房在所有票房中的排名加起來(lái),再除以電影總數(shù)。

除了「侏羅紀(jì)世界2」「***」「捉妖記2」,我都看過啦!

09 電影叫座不叫好***0

Python數(shù)據(jù)可視化:2018年電影分析

計(jì)算公式是,把某部電影的票房排名減去某部電影的評(píng)分排名加起來(lái),再除以電影總數(shù)。

可能是貓眼的用戶比較仁慈吧,與豆瓣相比,普遍評(píng)分都比較高。我個(gè)人都不太敢相信這個(gè)結(jié)果。

不過有一個(gè)還是挺準(zhǔn)的,「愛情公寓」。

10 電影類型分布

Python數(shù)據(jù)可視化:2018年電影分析

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

責(zé)任編輯:未麗燕 來(lái)源: 法納斯得
相關(guān)推薦

2018-12-05 13:59:45

電影分析爬蟲

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2021-10-11 08:04:22

Python數(shù)據(jù)行程

2018-12-03 16:50:23

數(shù)據(jù)可視化數(shù)據(jù)分析薪水

2019-01-02 11:59:26

Python數(shù)據(jù)可視化GDP

2018-11-21 14:09:31

可視化天氣霧霾

2017-12-21 11:08:18

2020-05-14 10:19:23

Python可視化分析

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2018-03-16 14:53:53

BI數(shù)據(jù)可視化工具

2016-12-29 20:05:56

數(shù)據(jù)可視化大數(shù)據(jù)產(chǎn)品分析

2017-01-12 17:28:59

數(shù)據(jù)分析數(shù)據(jù)可視化可視化

2022-08-26 09:15:58

Python可視化plotly

2017-02-07 15:54:14

數(shù)據(jù)可視化數(shù)據(jù)分析

2020-05-26 11:34:46

可視化WordCloud

2022-02-23 09:50:52

PythonEchartspyecharts

2017-03-09 09:54:13

分析數(shù)據(jù)可視化

2016-11-25 11:16:22

阿里支付寶數(shù)據(jù)分析

2023-11-24 14:02:00

Python數(shù)據(jù)分析

2017-10-31 09:38:53

大數(shù)據(jù)數(shù)據(jù)可視化Python
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)