Python教程

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

本文主要是介绍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()







这篇关于python爬虫爬取豆瓣影评并可视化的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!