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

python爬虫爬取豆瓣影评并可视化,第1张

概述#导入相关库frombs4importBeautifulSoupimportrequestsimportmatplotlib.pyplotaspltimportnumpyasnpimportreimportjiebaimportpandasaspdimportnumpyfromwordcloudimportWordCloudlen5=len4=len3=len2=len1=0type=['','&p
# 导入相关库from bs4 import BeautifulSoupimport requestsimport matplotlib.pyplot as pltimport numpy as npimport reimport jIEbaimport pandas as pdimport numpyfrom wordcloud import WordCloudlen5=len4=len3=len2=len1=0type = ['','&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:breakplt.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爬虫爬取豆瓣影评并可视化全部内容,希望文章能够帮你解决python爬虫爬取豆瓣影评并可视化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1185390.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存