目录
- 前言
- 分析
- 具体步骤
- 登录
- 爬取与存储
- 可视化分析
- 结语
前言
- 暑期档电影惨淡,但随着哪吒爆红开拓了新局面。这也是国产动画的首次爆红。在哪吒刚出,笔者
以为最多10亿
就算不错的了。没想过仅过了几天就破了10亿。接着头条又突破20亿--------目前11天27亿,势头增长依然很猛
!
那笔者就很好奇人们是怎么看待这一步电影的呢?
- 哪吒?我想哪吒是
陪伴过不少人
成长的一部动画片吧,也是记忆中算得上最好看
的动画片之一
了。里面的哪吒、小猪熊、申公豹、石鸡娘娘令人历历在目。我们或许都被哪吒的敢打敢为、勇敢和天真所感动
!
分析
具体步骤
登录
登录部分代码为:
import requests
import urllib.parse
from http import cookiejar
url='https://accounts.douban.com/j/mobile/login/basic'
header={'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
'Referer': 'https://accounts.douban.com/passport/login_popup?login_source=anony',
'Origin': 'https://accounts.douban.com',
'content-Type':'application/x-www-form-urlencoded',
'x-requested-with':'XMLHttpRequest',
'accept':'application/json',
'accept-encoding':'gzip, deflate, br',
'accept-language':'zh-CN,zh;q=0.9',
'connection': 'keep-alive'
,'Host': 'accounts.douban.com'
}
data={
'ck':'',
'name':'',
'password':'',
'remember':'false',
'ticket':''
}
##登录函数。post请求api。返回cookie。后面携带这个cookie访问接口
def login(username,password):
global data
data['name']=username
data['password']=password
data=urllib.parse.urlencode(data)
print(data)
req=requests.post(url,headers=header,data=data,verify=False)
cookies = requests.utils.dict_from_cookiejar(req.cookies)
print(cookies)
return cookies
爬取与存储
def getcomment(cookies):
start=0
w = xlwt.Workbook(encoding='ascii')
ws = w.add_sheet('sheet1')
index=1
while True:##存入ws.write(hang,lie,value)
try:
url = 'https://movie.douban.com/subject/26794435/comments?start='+str(start)+'&limit=20&sort=new_score&status=P&comments_only=1'
start+=20
req = requests.get(url,cookies=cookies)
res = req.json()
res=res['html']
soup = BeautifulSoup(res, 'lxml')
node = soup.select('.comment-item')
#print(node[0])
for va in node:
name = va.a.get('title')
star = va.select_one('.comment-info').select('span')[1].get('class')[0][-2]
comment = va.select_one('.short').text
print(name, star, comment)
ws.write(index,0,index)
ws.write(index, 1, name)
ws.write(index, 2, star)
ws.write(index, 3, comment)
index+=1
except Exception as e:
print(e)
break
w.save('nezha.xls')
可视化分析
- 我们要对评分进行统计、词频统计。还有就是生成词云展示。而对应的就是
matplotlib
、WordCloud
库。
评分统计:
词频统计:
词云展示:
代码
- 顺便给出可视化分析部分代码:
import matplotlib.pyplot as plt
import matplotlib
import jieba
import xlwt
import xlrd
from wordcloud import WordCloud
import numpy as np
from collections import Counter
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
##获取分数的饼图。用数组获取1-5分出现的次数
def anylasescore(comment):
score=[0,0,0,0,0,0]
count=0
for va in comment:
try:
score[int(va[2])]+=1
count+=1
except Exception as e:
continue
print(score)
label='1分','2分','3分','4分','5分'
color = 'blue', 'orange', 'yellow', 'green', 'red' # 各类别颜色
size=[0,0,0,0,0]
explode=[0,0,0,0,0]
for i in range(1,5):
size[i]=score[i]*100/count
explode[i]=score[i]/count/10
pie = plt.pie(size, colors=color, explode=explode, labels=label, shadow=True, autopct='%1.1f%%')
for font in pie[1]:
font.set_size(8)
for digit in pie[2]:
digit.set_size(8)
plt.axis('equal')
plt.title(u'各个评分占比', fontsize=12)
plt.legend(loc=0, bbox_to_anchor=(0.82, 1)) # 图例
# 设置legend的字体大小
leg = plt.gca().get_legend()
ltext = leg.get_texts()
plt.setp(ltext, fontsize=6)
plt.savefig("score.png")
# 显示图
plt.show()
def getzhifang(map):##词频的直方图
x=[]##词语
y=[]##词语出现数量
for k,v in map.most_common(15):
x.append(k)
y.append(v)
Xi = np.array(x)
Yi = np.array(y)
x = np.arange(0, 15, 1)
width = 0.6
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.figure(figsize=(8, 6)) ##指定图像比例: 8:6
plt.bar(Xi, Yi, width, color='blue', label='热门词频统计', alpha=0.8,)
plt.xlabel("词频")##标签
plt.ylabel("次数")
plt.show()
return
def getciyun_most(map):##获取词云
x = []
y = []
for k, v in map.most_common(300):##300个词云分2个词云
x.append(k)
y.append(v)
xi=x[0:150]
xi=' '.join(xi)
print(xi)
backgroud_Image = plt.imread('nezha.jpg') # 如果需要个性化词云,哪吒背景图
wc = WordCloud(background_color="white",
width=1500, height=1200,
#min_font_size=40,
mask=backgroud_Image,
font_path="simhei.ttf",
max_font_size=150, # 设置字体最大值
random_state=50, # 设置有多少种随机生成状态,即有多少种配色方案
) # 字体这里有个坑,一定要设这个参数。否则会显示一堆小方框wc.font_path="simhei.ttf" # 黑体
# wc.font_path="simhei.ttf"
my_wordcloud = wc.generate(xi)
plt.imshow(my_wordcloud)
my_wordcloud.to_file("img.jpg")
xi=' '.join(x[150:300])
my_wordcloud = wc.generate(xi)
my_wordcloud.to_file("img2.jpg")
plt.axis("off")
def anylaseword(comment):## 分词,去掉符号、换行等垃圾数据
commnetstr=''
c = Counter()
low=Counter()
index=0
for va in comment:
seg_list = jieba.cut(va[3],cut_all=False)
index+=1
for x in seg_list:
if len(x) > 1 and x != '\r\n':
try:
c[x]+=1
except:
continue
commnetstr+=va[3]
for (k, v) in c.most_common():
if v<5:
c.pop(k)
continue
#print(k,v)
print(len(c),c)
getzhifang(c)
getciyun_most(c)
#print(commnetstr)
def anylase():
data = xlrd.open_workbook('nezha.xls') # 打开xls文件
table = data.sheets()[0] # 打开第i张表
comment = []
for i in range(1, 500):
comment.append(table.row_values(i))
# print(comment)
anylasescore(comment)
anylaseword(comment)
if __name__ == '__main__':
anylase()
结语
- 如果自己需要可以到github下载项目完整代码。当然,只需要更改部分即可同理分析其他电影。
- 项目
依然有不够完善地方
,如影评,对不同评分的平均不同处理、其他不同角度如评论用户性别、地点等等等等,这里不做延申。 - 如果对
后端、爬虫、数据结构算法
等感性趣欢迎关注我的个人公众号交流(关注一波十年少):bigsai
持续输出分享!