python爬取豆瓣评论制作词云代码

作者:大学生编程地 时间:2023-03-14 04:31:40 

一、爬取豆瓣热评

该程序进行爬取豆瓣热评,将爬取的评论(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爬取豆瓣评论制作词云代码

来源:https://blog.csdn.net/qq_46500711/article/details/122288753

标签:python,豆瓣,词云
0
投稿

猜你喜欢

  • Python函数基础实例详解【函数嵌套,命名空间,函数对象,闭包函数等】

    2023-12-10 15:45:59
  • Tensorflow进行多维矩阵的拆分与拼接实例

    2021-11-29 22:20:58
  • 代码分析Python地图坐标转换

    2022-01-15 19:50:48
  • Oracle 查看表空间的大小及使用情况sql语句

    2023-07-14 20:06:31
  • 详解python __init__.py 和 __all__作用

    2023-08-22 06:55:22
  • Python配置mysql的教程(推荐)

    2024-01-21 00:40:19
  • Python MOCK SERVER moco模拟接口测试过程解析

    2023-12-15 06:37:00
  • SQL语句操作主从关系表

    2011-06-19 13:19:05
  • Python if else条件语句形式详解

    2021-09-21 06:48:24
  • Python实现自动驾驶训练模型

    2023-07-28 18:45:29
  • 详解pyqt5的UI中嵌入matplotlib图形并实时刷新(挖坑和填坑)

    2023-01-04 22:01:05
  • Python实战购物车项目的实现参考

    2021-09-28 11:06:29
  • javascript设计模式 – 原型模式原理与应用实例分析

    2024-04-22 13:26:50
  • 详解Python2.x中对Unicode编码的使用

    2022-12-01 10:58:04
  • vue实现滑动验证条

    2024-04-30 10:29:14
  • 整理Python中的赋值运算符

    2021-10-30 16:25:21
  • Python+radar实现随机日期时间的生成

    2021-04-16 23:22:57
  • go语言yaml转map、map遍历的实现

    2024-05-25 15:19:52
  • Python 函数那不为人知的一面

    2022-09-24 10:03:31
  • RDFa介绍——构建更友好的web页面

    2009-09-19 17:01:00
  • asp之家 网络编程 m.aspxhome.com