python爬取豆瓣评论制作词云代码
2022-01-06 04:07
896 查看
目录
一、爬取豆瓣热评
该程序进行爬取豆瓣热评,将爬取的评论(json文件)保存到与该python文件同一级目录下
注意需要下载这几个库:requests、lxml、json、time
import requests from lxml import etree import json import time class Spider(object): def __init__(self): #seif.ure='https://movie.douban.com/subject/23885074/reviews?start=0' self.headers={ 'User-Agent':'Mozilla/5.0(Windows NT6.1;Win64;x64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/75.0.3700.100Safari/537.36' } def get_data(self,url): response = requests.get(url,headers=self.headers).content.decode('utf-8') page=etree.HTML(response)#xpath 对象 #获取所有数据节点 node_list = page.xpath('//div[@class="review-list "]/div') for node in node_list: #作者 author = node.xpath('.//header[@class="main-hd"]//a[2]/text()')[0] #评论 text = node.xpath('string(.//div[@class="main-bd"]//div[@class="short-content"])') items={ 'author':author, 'text':text.strip() } #持久化存储 with open('yewen.json','a',encoding='utf-8') as f: f.write(json.dumps(items,ensure_ascii=False)+'\n') def run(self): for i in range(1,47): url='https://movie.douban.com/subject/26885074/reviews?start{}'.format(i*20) print('正在爬取第{}页'.format(i)) self.get_data(url) time.sleep(3) if __name__=='__main__': s=Spider() s.run()
二、制作词云
该程序将json中的数据进行处理,提取重要信息,并用wordcloud库制作词云图片,同样保存到与该python文件同一级目录下
注意需要下载这几个库:jieba、wordcloud、json
import jieba from wordcloud import WordCloud import json f= open("yewen.json", "r", encoding="utf-8") data_list= f.readlines() str ='' for data in data_list: text= json.loads(data)['text'] str +=text #替换无关紧要的词语 result_str = str.replace('展开', '').replace('这篇','').replace('影评','').replace('电影','').replace('这部', '').replace('可能', '').replace('剧情','') cut_text = jieba.lcut(result_str) result = " ".join(cut_text) wc = WordCloud(font_path='simhei.ttf', background_color="white", max_words=600, width=1000, height=1000, min_font_size=20, max_font_size=100,) #mast=plt.imreda('snake.jpg')#背景图片 wc.generate(result)#转化为词云的操作 wc.to_file("text.jpg")#保存 f.close()
总结
到此这篇关于python爬取豆瓣评论制作词云代码的文章就介绍到这了,更多相关python爬取豆瓣评论内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:相关文章推荐
- python——代码制作数据表
- python代码制作configure文件示例
- Python进度条的制作代码实例
- Python 爬虫实战(1):分析豆瓣中最新电影的影评并制作词云
- python3批量删除豆瓣分组下的好友的实现代码
- python爬虫——json格式爬取微博评论及评论人信息(含代码)
- [转]截屏法制作pdf——更新python代码和效果图
- python代码制作configure文件的例子
- [超详细] Python3爬取豆瓣影评、去停用词、词云图、评论关键词绘图处理
- 超全代码详解 Python 制作精美炫酷图表教程
- 一行Python代码制作动态二维码的实现
- Python3 豆瓣登录并评论,保存登录状态
- 用python的selenium模块自动爬取周董歌曲评论和登录豆瓣
- 【Python成长之路】超简单的词云图制作,仅十几行代码哟
- 豆瓣机器人 自动加入/退出小组、自动在小组发帖/删帖、自动回复 Python实现代码 豆瓣爬虫
- Python爬虫(三)——对豆瓣图书各模块评论数与评分图形化分析
- python爬虫实例,一小时上手爬取淘宝评论(附代码)
- 简单几行代码,python制作美图秀秀软件!
- 几行代码教你使用python制作词云(四)
- 【python个人项目】python 爬淘宝评论文字、图片并且自定义制作优惠券图片