ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-06-02 14:31:16  阅读:220  来源: 互联网

标签:rating plt span python 爬虫 爬取 soup import find


# 导入相关库
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()







标签:rating,plt,span,python,爬虫,爬取,soup,import,find
来源: https://blog.csdn.net/capsize/article/details/117467052

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有