其他
Scrapy Python爬虫实战:抓取知乎问题下所有回答!
嗨!大家好,我是酷头
欢迎来到学习python的宝藏基地~~~
创建scrapy项目
前面教程概念讲的我嘴都麻了,估计大家看得也快烦死了,
直接进入主题吧!
项目创建完成结构如下:
需求分析
网页分析
item定义
# 作者首页
photo = scrapy.Field()
# 作者名称
name = scrapy.Field()
# 评论时间
cmt_time = scrapy.Field()
# 评论点赞数
stars = scrapy.Field()
# 评论内容
comments = scrapy.Field()
发送请求
def parse(self, response):
zhihu_data = response.json()
ic(zhihu_data)
可以看到,我们已经成功的获取到了该json格式数据集
for zh in zhihu:
# 作者首页
item['photo'] = 'https://www.zhihu.com/people/' + zh['author']['member']['url_token']
# 作者名称
item['name'] = zh['author']['member']['name']
# 评论时间
cmt_time = zh['created_time']
item['cmt_time'] = time.strftime('%Y-%m-%d %H:%M', time.localtime(cmt_time)).split(' ')[0]
# 评论点赞数
item['stars'] = zh['vote_count']
# 评论内容
item['comments'] = zh['content']
'''
ic| item: {'cmt_time': '2021-06-24',
'comments': '雨衣?那个雨衣[捂脸]',
'name': '菜鸟教程',
'photo': 'https://www.zhihu.com/people/li-shi-yi-372tian',
'stars': 484}
ic| item: {'cmt_time': '2021-06-24',
'comments': '众所周知嘛,雨衣依然是下雨天穿的衣服[蹲]',
'name': '枫叶',
'photo': 'https://www.zhihu.com/people/die-qi-hua',
'stars': 187}
ic| item: {'cmt_time': '2021-06-24',
'comments': '基本是单身狗写出来的,但凡真的谈恋爱的,不会这么做!<br>大家看看就得了,适合自己的才是最好的。<br>总之,记得适度而行!',
'name': '莫小风',
'photo': 'https://www.zhihu.com/people/mo-xiao-37-62',
'stars': 804}
ic| item: {'cmt_time': '2021-06-24',
'comments': '<br><br>她小脸胖乎乎我可喜欢,我俩从大一处到现在马上大四了。祝您和您对象也长长久久,如果没有就找一个。谢谢您! '
'<br><br>如果里面有内容容易引起误会和歧视或者可能带来风险,你可以拿出来提醒大家,而不是张口就来。谢谢您!<br><br>',
'name': '枫叶',
'photo': 'https://www.zhihu.com/people/die-qi-hua',
'stars': 402}
ic| item: {'cmt_time': '2021-06-24',
'comments': '道路千万条,安全第一条,雨衣不常备,情侣两行泪。',
'name': 'Jacob',
'photo': 'https://www.zhihu.com/people/jacob-hu',
'stars': 1405}
ic| item: {'cmt_time': '2021-06-25',
'comments': '小孩嗝屁袋',
'name': '王当心',
'photo': 'https://www.zhihu.com/people/mei-li-de-zheng-zha',
'stars': 986}
ic| item: {'cmt_time': '2021-06-26',
'comments': '现在知乎第一句不搞颜色是没有热度吗?[好奇]',
'name': 'danger soul',
'photo': 'https://www.zhihu.com/people/danger-soul-59',
'stars': 689}
'''
ITEM_PIPELINES = {
'zhihu.pipelines.ZhihuPipeline': 300,
}
多页获取
https://www.zhihu.com/api/v4/answers/1935351817/root_comments?order=normal&limit=20&offset=0&status=open
https://www.zhihu.com/api/v4/answers/1935351817/root_comments?order=normal&limit=20&offset=20&status=open
https://www.zhihu.com/api/v4/answers/1935351817/root_comments?order=normal&limit=20&offset=40&status=open
for page in range(1, 100 + 1):
url = f'https://www.zhihu.com/api/v4/answers/1935351817/root_comments?order=normal&limit=20&offset={(page-1)*20}&status=open'
数据下载
wb = Workbook()
ws = self.wb.active
ws.append(['作者首页', '作者名称', '评论时间', '点赞人数', '评论内容'])
line = [item['photo'], item['name'], item['cmt_time'], item['stars'], item['comments']]
ws.append(line)
wb.save('../知乎.xlsx')
词云展示
我们使用stylecloud来绘图,部分代码如下:
有兴趣的小伙伴可以参考:
c_title = exist_col['评论内容'].tolist()
# 观影评论词云图
wordlist = jieba.cut(''.join(c_title))
result = ' '.join(wordlist)
# 设置停用词
stop_words = ['的', '是', '看', '了', '你', '我', '吗', '在', '没', '吗', '这', '不', '就', '人', '好', '有', '都', '什么', '没有']
pic = '../img.jpg'
gen_stylecloud(text=result,
icon_name='fab fa-python',
font_path='msyh.ttc',
background_color='white',
output_name=pic,
custom_stopwords=stop_words
)
print('知乎词云图绘制成功!')
知乎词频图
这里我们使用熊猫来读取数据,并去除空行。使用jieba制作分词
有兴趣的小伙伴可以参考
知乎评论出现频率最高的前十个词分别如下:
# 词频设置
all_words = [word for word in result.split(' ') if len(word) > 1 and word not in stop_words]
wordcount = Counter(all_words).most_common(10)
'''
('雨衣', '思考', '一起', 'br', '对象', '捂脸', '女朋友', '可以', '一个', '就是')
(62, 58, 48, 42, 29, 28, 26, 22, 20, 18)
'''
词频图如下:
最大值62,最小值18,均值29
评论点赞最多
我们使用pandas找出点赞第一的评论如下:
# 读取数据
pd_data = pd.read_excel('../知乎.xlsx')
# 最多点赞数
max_star = pd_data[pd_data['点赞人数'] == pd_data['点赞人数'].max()]
print(max_star)
'''
作者首页 作者名称 评论时间 点赞人数 评论内容
https://www.zhihu.com/people/jacob-hu Jacob 2021-06-24 1405 道路千万条,安全第一条,雨衣不常备,情侣两行泪。
'''
自古人才出评论区,这话一点不差啊,
有对象的老板能给我解释这句话什么意思嘛
情感分析
# 情感分析
pinglun = '道路千万条,安全第一条,雨衣不常备,情侣两行泪。'
Sentiment_analysis = SnowNLP(pinglun).sentiments
print(Sentiment_analysis)
'''
0.19973577495669226
'''
得到的情感值为 0.19973577495669226,情感值较低
各位老湿机还是安全驾驶啊!
对此,你有什么看法?