安徽鲲鹏建设集团有限公司网站,建立个人网站代码,如何提高网站转化率,佛山市企业网站建设报价嗨害大家好鸭#xff01;我是小熊猫~
总有那么一句银幕台词能打动人心
总有那么一幕名导名作念念不忘
不知道大家有多久没有放松一下了呢#xff1f;
本次就来给大家采集一下某瓣电影并做词云分析
康康哪一部才是大家心中的经典呢#xff1f; 最近又有哪一部可能会成为…嗨害大家好鸭我是小熊猫~
总有那么一句银幕台词能打动人心
总有那么一幕名导名作念念不忘
不知道大家有多久没有放松一下了呢
本次就来给大家采集一下某瓣电影并做词云分析
康康哪一部才是大家心中的经典呢 最近又有哪一部可能会成为经典呢 源码、素材python永久安装包:点击此处跳转文末名片获取 环境使用: Python 3.8 解释器 Pycharm 编辑器
模块使用 import parsel pip install parsel import requests pip install requests import csv 代码展示
# 导入数据请求模块 -- 第三方模块, 需要安装 pip install requests
import requests
# 导入数据解析模块 -- 第三方模块, 需要安装 pip install parsel
import parsel
# 导入csv模块 -- 内置模块, 不需要安装
import csv安装模块: win R 输入cmd 然后输入 安装命令 pip install requests 在pycharm终端里面 输入安装命令 pip install requests 模拟浏览器: -- headers 请求头 开发者工具进行复制 把python代码伪装成浏览器去发送请求 目的: 为了防止被反爬 反爬: 你得不到数据, 或者返回的数据不是你想要的 如何批量替换 选中替换内容, ctrl R 勾选上 .* 正则 输入正则匹配规则, 进行替换 :.* 采集的速度过快/频繁, 可能会IP异常
解决方法:
登陆账号加上cookie用IP代理, 切换IP 免费的IP, https 可能用不了 HTTP有一些可以的, 质量不好 氪金的IP 一个IP 几分钱一个感兴趣的可以翻一翻我之前的文章 多页的数据采集 分析请求链接的变化规律 正式代码
1. 发送请求, 模拟浏览器对于url地址发送请求
# 0起始数包含, 201末尾数不包含, 20步长
for page in range(0, 201, 20):# 请求链接 字符串格式化方法 --url fhttps://movie.****.com/subject/4811774/comments?start{page}limit20statusPsortnew_score# 伪装模拟headers {# User-Agent 用户代理, 表示浏览器基本身份信息User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36}# 发送请求response requests.get(urlurl, headersheaders)2. 获取数据, 获取服务器返回响应数据
开发者工具: responseresponse.text -- 获取响应文本数据response -- 响应对象text -- 文本# 把获取下来html字符传数据response.text, 转换可解析的对象selector parsel.Selector(response.text)3. 解析数据, 提取我们想要的数据内容
影评相关数据
css选择器: 根据标签属性提取数据内容 # 第一次提取所有内容divs selector.css(div.comment-item)# 把列表里面元素一个一个提取出来, for循环遍历for div in divs:湖南 长沙.comment-info a::text -- 提取类名为comment-info标签下面a标签里面文本数据get() -- 获取第一个标签数据, 返回字符串数据类型attr() -- 获取标签里面属性name div.css(.comment-info a::text).get() # 昵称rating div.css(.rating::attr(title)).get() # 评分date div.css(.comment-time ::attr(title)).get() # 日期area div.css(.comment-location::text).get() # 归属地short div.css(.short::text).get().replace(\n, ) # 评论count div.css(.vote-count::text).get() # 有用# 把数据放到字典里面dit {昵称: name,评分: rating,日期: date,归属地: area,评论: short,有用: count,}# 写入数据csv_writer.writerow(dit)print(name, rating, date, area, short, count)
创建文件对象
f open(影评.csv, modea, encodingutf-8-sig, newline)
# f 文件对象 fieldnames 表头/字段名
csv_writer csv.DictWriter(f, fieldnames[昵称,评分,日期,归属地,评论,有用,
])# 写入表头
csv_writer.writeheader()
绘制词云图 模块导入
# 导入结巴模块 -- 第三方模块, 需要安装 pip install jieba
import jieba
# 导入pandas -- 第三方模块, 需要安装 pip install pandas
import pandas as pd
# 导入词云模块 -- 第三方模块, 需要安装 pip install wordcloud
import wordcloud
读取csv表格里面数据内容
df pd.read_csv(影评.csv)
获取评论内容
content_list df[评论].to_list()
# 把列表转成字符串
content .join(content_list)
# 进行分词处理
string .join(jieba.lcut(content))
词云图配置
wc wordcloud.WordCloud(width1000, # 宽height700, # 高background_colorwhite, # 背景颜色font_pathmsyh.ttc, # 设置字体stopwords{了, 的, 是, 我, 在, 和},scale15
)
# 传入文字内容
wc.generate(string)
# 输出词云图
wc.to_file(词云图.png)
print(string)
本篇文章就到这里啦~
我是小熊猫咱下篇文章再见啦(✿◡‿◡) 问题解答 · 源码获取 · 技术交流 · 抱团学习请联系 文章转载自: http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.htbsk.cn.gov.cn.htbsk.cn http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.rjyd.cn.gov.cn.rjyd.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn http://www.morning.lrzst.cn.gov.cn.lrzst.cn http://www.morning.qwlml.cn.gov.cn.qwlml.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.pgcmz.cn.gov.cn.pgcmz.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.bgrsr.cn.gov.cn.bgrsr.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.nzfjm.cn.gov.cn.nzfjm.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.xqcgb.cn.gov.cn.xqcgb.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.dyzbt.cn.gov.cn.dyzbt.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.hrzymy.com.gov.cn.hrzymy.com http://www.morning.krtky.cn.gov.cn.krtky.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.gmgnp.cn.gov.cn.gmgnp.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.pwppk.cn.gov.cn.pwppk.cn