python爬取3萬+條評論,解讀貓眼評分9.5的《海王》是否值得一看?


 

海王

前言

2018年12月7日,本年度最后一部壓軸大片《海王》如期上映,目前貓眼評分達到9.5分,靠着1.5億美金的制作成本,以小博大,目前票房接近9億,本文爬取了貓眼3w+條評論,多方位帶你解讀是否值得一看!!其實(yin)我(wei)也(mei)沒(qian)看!

 

 除了這個案例。我還會在裙里分享各種有趣的python項目案例視頻教程,有興趣的可以來我的python學習免肥解答.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,這里還有資深程序員分享以前學習心得,學習筆記,還有一線企業的工作經驗等
海王

數據爬取

現在貓眼電影網頁似乎已經全部服務端渲染了,沒有發現相應的評論接口,參考了之前其他文章中對於貓眼數據的爬取方法,找到了評論接口!
http://m.maoyan.com/mmdb/comments/movie/249342.json?v=yes&offset=15&startTime=2018-1208%2019%3A17%3A16%E3%80%82

 
檢查網頁發現無評論鏈接.png

 

接口有了,但是沒有對應的電影id,不過這難不倒我們,使用貓眼app+charles,我們成功找到海王對應的電影ID;

 

 
電影id獲取

 

接下來爬取評論:

#獲取數據 def get_data(url): headrs = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" } html = request(method='GET',url=url,headers=headrs) if html.status_code == 200: return html.content else: return None 

解析接口返回數據

#處理接口返回數據 def parse_data(html): json_data = json.loads(html,encoding='utf-8')['cmts'] comments = [] try: for item in json_data: comment = { 'nickName':item['nickName'], 'cityName':item['cityName'] if 'cityName' in item else '', 'content':item['content'].strip().replace('\n',''), 'score':item['score'], 'startTime': item['startTime'] } comments.append(comment) return comments except Exception as e: print(e) 

處理鏈接及存儲數據

def change_url_and_save(): start_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())).replace(' ','%20') end_time = '2018-12-07 00:00:00' while start_time > end_time: url = "http://m.maoyan.com/mmdb/comments/movie/249342.json?v=yes&offset=15&startTime="+start_time html = None try: html = get_data(url) except Exception as e: time.sleep(0.5) html = get_data(url) else: time.sleep(0.1) comments = parse_data(html) start_time = comments[14]['startTime'] print(start_time) t = datetime.datetime.now() start_time = time.strptime(start_time,'%Y-%m-%d %H:%M:%S') start_time = datetime.datetime.fromtimestamp(time.mktime(start_time))+datetime.timedelta(seconds=-1) start_time = time.mktime(start_time.timetuple()) start_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(start_time)).replace(' ', '%20') for item in comments: print(item) with open('/Users/mac/Desktop/H5DOC/H5learn/REPTILE/comments.txt', 'a', encoding='utf-8')as f: f.write(item['nickName'] + ',' + item['cityName'] + ',' + item['content'] + ',' + str(item['score']) +','+ item[ 'startTime'] + '\n') 

最終我們獲取到了大約33000條數據

 

 
評論數據.png

數據分析

數據分析我們使用了百度的pyecharts、excel以及使用wordcloud生成詞雲
首先看一下,評論分布熱力圖:

 

 
觀眾分布熱力圖

 

京津冀、長三角、珠三角等在各種榜單長期霸榜單的區域,在熱力圖中,依然占據着重要地位。而新一線的川渝、鄭州武漢緊隨其后!
下面是評論數前20的城市

 

 
評論數主要分布城市

評論全國分布圖:

 

 
評論分布城市

 

由圖中可以看出基本與熱力圖相似,主要分布在各大一線、新一線城市,對於杭州為何會排在第17的位置,我覺得可能是阿里大本營,大家都用淘票票的緣故吧!😄😄
接下來是評分占比情況

 

 
評分占比.png

由圖中可以看出,評分在4以上的占比達到了94%,而平均評分也達到4.68分!!!
再來看一下各城市評分情況:

 

 
各城市評分

看了評分再來看看評論的詞雲情況:

 

 
哈哈哈

 

 
詞雲1

 

 
詞雲2

 

詞雲出現較多的是好看、特效、劇情、震撼等,可以看出大家對此電影對特效和劇情還是十分認同的,畢竟爛番茄新鮮度73%,1.5億美元對制作能做到如此實屬不易,我還是決定這周末去影院刷一下的!

詞雲代碼

def data_wordclound(): comments = '' with open('comments.txt','r') as f: rows = f.readlines() try: for row in rows: lit = row.split(',') if len(lit) >= 3: comment = lit[2] if comment != '': comments += ' '.join(jieba.cut(comment.strip())) # print(comments) except Exception as e: print(e) hai_coloring = imread('hai.jpeg') # 多慮沒用的停止詞 stopwords = STOPWORDS.copy() stopwords.add('電影') stopwords.add('一部') stopwords.add('一個') stopwords.add('沒有') stopwords.add('什么') stopwords.add('有點') stopwords.add('感覺') stopwords.add('海王') stopwords.add('就是') stopwords.add('覺得') stopwords.add('DC') bg_image = plt.imread('hai.jpeg') font_path = '/System/Library/Fonts/STHeiti Light.ttc' wc = WordCloud(width=1024, height=768, background_color='white', mask=bg_image, font_path=font_path, stopwords=stopwords, max_font_size=400, random_state=50) wc.generate(comments) images_colors = ImageColorGenerator(hai_coloring) plt.figure() plt.imshow(wc.recolor(color_func=images_colors)) plt.axis('off') plt.show() 

綜上,我覺得沒看的小伙伴可以跟我一樣一起周末去貢獻一下票房了!哈哈哈,除了這個案例。我還會在裙里分享各種有趣的python項目案例視頻教程,有興趣的可以來我的python學習免肥解答.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,這里還有資深程序員分享以前學習心得,學習筆記,還有一線企業的工作經驗等

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM