学堂 学堂 学堂公众号手机端

[原创]python-豆瓣电影《肖申克的救赎》评论数据爬取(原创)

lewis 6年前 (2019-06-12) 阅读数 11 #技术

最终词图效果:

第一步:豆瓣电影《肖申克的救赎》评论数据(​​https://movie.douban.com/subject/1292052/comments?sort=time&status=P)​​

第二步:词频统计可视化展示

第三步:绘制词云图

第四步:

================================================================

配置准备

================================================================

#安装jieba分词和词云

pipinstalljieba

pipinstallwordcloud

#安装paddle

pipinstall--upgradePaddlePaddle

#安装模型

#hubinstallporn_detection_lstm==1.1.0

pipinstall--upgradepaddlehub

pipinstallnumpy

#安装Beautifulsoup

pipinstallBeautifulSoup4

问题:

1.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 1: invalid continuation byte

解决方法:

1.不使用urlLib换做requests

2.去掉请求头中的 'Accept-Encoding': 'gzip, deflate, br'

3.返回值reponse 转字符串指定编码utf-8

# 'Accept-Encoding': 'gzip, deflate, br',

2.关于cookie

解决方法:

1.去豆瓣请求头中复制cookie设置到请求头中

'Cookie': 'bid=WD6_t6hVqgM'

3.请求返回418的问题

解决方案模拟设置请求头,设置user-agent

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',

4.使用beautifulsoup获取不到评论

解决方法:

第一步:指定解析参数为'lxml'

soupComment = BeautifulSoup(html, 'lxml')

第二步:

findAll方法指定css文件的class名

print('网页内容:', soupComment.prettify()) comments = soupComment.findAll(class_='short')

源码地址:

​​https://gitee.com/mikite/python_sp_shawshank​​

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门