用数据揭秘《一出好戏》好在哪里

黄渤导演的处女作《一出好戏》在八月十号上映,两天累计票房4.37亿,豆瓣评分7.4,网友一片叫好,没想到拍出了一部魔幻现实主义的故事。下面我们用数据来展示一下网友的看法。

获取数据

这里选择用猫眼来获取网友的评论
先来分析一下网页,打开猫眼一出好戏页面,发现只有几条评论。

影评

影评


猫眼在没有登录的情况下并没有展现评论内容,我们换一种思路,用电脑模拟手机客户端来获取数据。

 


点击箭头所指可以看到,已经在模拟手机端,这样我们就能看到影评数据了。
接下来我们点击查看全部影评,并没有发现页面跳转,这显然是Ajax动态加载,我们查看一下参数:
参数

参数


我们可以构造参数,也可以查看他的url

   for i in range(900):
        url = 'http://m.maoyan.com/mmdb/comments/movie/1203084.json?_v_=yes&offset={}'.format(str(i))

最后我们利用这种方式构造url。
接下来解析数据,以及保存数据:

class Spider():

    def get_page(self,url,headers):
        try:
            response = requests.get(url,headers=headers)
            if response.status_code == 200:
                return response.json()
            else:
                return None
        except Exception:
            return None

    def parse_page(self,html):
        for item in html['cmts']:
            yield{
                'content_name':item['time'].split(' ')[0],
                'city':item['cityName'],
                'contemt':item['content'],
                'score':item['score'],
            }

    def save_to_txt(self,data):
        #保存数据
        with open('data.txt','a',encoding='utf-8') as f:
            f.write(json.dumps(data,ensure_ascii=False) + '\n')

    def run(self):
        for i in range(900):
            url = 'http://m.maoyan.com/mmdb/comments/movie/1203084.json?_v_=yes&offset={}'.format(str(i))
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64)'
                                     ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
            html = self.get_page(url,headers)
            for data in self.parse_page(html):
                self.save_to_txt(data)

数据处理

爬取的数据有些是重复的,我们要清洗一下:

def qu_chong():
    outopen = open('data.txt', 'r', encoding='utf-8')
    inopen = open('data_new.txt', 'a', encoding='utf-8')
    L = []
    for line in outopen.readlines():
        if line not in L:
            L.append(line)
            inopen.write(line)

    outopen.close()
    inopen.close()

if __name__ == '__main__':
    qu_chong()

词云展示

接下来我们用结巴库和wordcloud库,把网友评论制成词云,来看看网友对一出好戏的评价

词云图.jpg

词云图.jpg


可以看到,其中「不错」、「人性」、「值得一看」占了网友大多数评论,看来网友对《一出好戏》评价蛮不错的。还有网友说张艺兴演技炸裂,确实在这部剧中演的很好。
下面是代码

import jieba
import wordcloud
from scipy.misc import imread

comment = []
with open('data_new.txt','r',encoding='utf-8') as f:
    for item in f.readlines():
        comment.append(item.split(',')[2].replace('\n',''))


comment_ci = jieba.lcut(str(comment),cut_all=False)
txt = ' '.join(comment_ci)
w = wordcloud.WordCloud(font_path='msyh.ttc',width=1000,max_font_size=100,font_step=2,
                        height=700,max_words=600,stopwords={'content','contemt','电影','剧情','有点'})

w.generate(txt)
w.to_file('词云图.jpg')

星级分布

接下来看看网友的平分

评分

评分


代码

from pyecharts import Pie
from pyecharts import Bar

rates = []
#获取评分数据
with open('data_new.txt','r',encoding='utf-8') as f:
    for line in f.readlines():
        rates.append(line.split(',')[-1][10:].replace('}\n',''))

v5 = rates.count('5') + rates.count('4.5')
v4 = rates.count('4') + rates.count('3.5')
v3 = rates.count('3') + rates.count('2.5')
v2 = rates.count('2') + rates.count('1.5')
v1 = rates.count('1') + rates.count('0.5')
attr = ['五星','四星','三星','二星','一星']
v= [v5,v4,v3,v2,v1]

bar = Bar('数据来源:','公众号sixkery')
bar.add('一出好戏',attr,v)
bar.render()

可以看到,大部分的网友都非常支持黄导。
如果你还没有看,正好趁着周末放松一下吧。

猜你喜欢

转载自blog.csdn.net/sixkery/article/details/81606726