python爬虫爬取豆瓣影评并可视化

# 导入相关库
from bs4 import BeautifulSoup
import requests
import matplotlib.pyplot as plt
import numpy as np
import re
import jieba
import pandas as pd
import numpy
from wordcloud import WordCloud


len5=len4=len3=len2=len1=0
type = ['','&percent_type=h','&percent_type=m','&percent_type=l']
for i in range(0,101,20):
    for j in range(4):
        url = 'https://movie.douban.com/subject/26266893/comments?start={start}&limit=20&sort=new_score&status=P{t}'.format(start=i,t=type[j])
        headers = {  # 模拟浏览器请求,可以用 cookie模拟登陆状态
            'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
        }


        data = requests.get(url, headers=headers)  # 使用requests向服务器发起get请求,通过header模拟浏览器行为
        soup = BeautifulSoup(data.text, 'lxml')  # 将服务器返回的对象使用BeautifulSoup解析,wb_data为response对象,需要文本化
# 确定要抓取的元素位置
        if j == 0:
            len5 += len(soup.find_all('span',class_='allstar50 rating'))
            len4 += len(soup.find_all('span',class_='allstar40 rating'))
            len3 += len(soup.find_all('span',class_='allstar30 rating'))
            len2 += len(soup.find_all('span',class_='allstar20 rating'))
            len1 += len(soup.find_all('span',class_='allstar10 rating'))

        for tt in soup.find_all('span',class_="short"):
            if tt.string!=None:
                # print(tt.string)
                save_path = 'F:\\PycharmDemo\Project\DouBan'
                save_name = '\\影评{}'.format(type[j]) + '.txt'
                full_path = save_path + save_name
                # fp = open(full_path, 'a+')
                fp = open(full_path, 'a+', encoding='utf-8')

                fp.write(tt.string + '\n')
            else:break

plt.figure(figsize=(8, 6), dpi=80)
# 再创建一个规格为 1 x 1 的子图
plt.subplot(1, 1, 1)
# 绘制柱状图, 每根柱子的颜色为紫罗兰色
p2 = plt.bar(np.arange(5),[len1, len2, len3, len4, len5],width=0.35,color="#87CEFA")
# 设置横轴标签
plt.xlabel('star')
# 设置纵轴标签
plt.ylabel('value')
# 添加标题
plt.title('rating')
# 添加纵横轴的刻度
plt.xticks(np.arange(5), ('1', '2', '3', '4', '5'))
plt.yticks(np.arange(0, 40, 1))
plt.savefig('F://PycharmDemo/Project/DouBan/picture1.jpg',dpi=500)
plt.show()

name_list = ['good', 'bad', 'normal']
num_list = [len5+len4,len3,len1+len2]
# 保证圆形
plt.pie(x=num_list, labels=name_list, autopct='%3.1f %%')
plt.savefig('F://PycharmDemo/Project/DouBan/picture2.jpg',dpi=500)
plt.show()







  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隼尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值