爬虫实战 | 手把手用Python教你采集&可视化知乎问题的回答(内附代码)

110 篇文章 1 订阅
52 篇文章 0 订阅

在这里插入图片描述
在这里插入图片描述

爬虫设计流程

1、探寻网址规律
2、尝试对某一网页访问
3、解析感兴趣的数据
4、存储到csv
5、整理汇总代码

一、探寻网址规律

1、按F12键打开 开发者工具,
2、选中network面板,点击 查看全部6217个回答
3、准备观察开发者工具中的监测到的网址
4、对每个网址经过下图456操作
5、点击preview
6、查看content与当前页面的回答是否一致
7、最终发现网址如7中的红色方框,请求方式为GET方法

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

8、依旧是7所在的页面,滑动到最下方,可以看到offset和limit
发现的网址(注意最后一行的offset)

https://www.zhihu.com/api/v4/questions/432119474/answers?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled%3B&offset=3&limit=5&sort_by=default&platform=desktop

中也存在offset,该单词的意思是偏移量。

  • offset 我猜测该值类似于page页面数
  • limit 每个url能展现多少个回答,默认5个。

网址模板(注意模板内最后一行offset)

https://www.zhihu.com/api/v4/questions/432119474/answers?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled%3B&offset={offset}&limit=5&sort_by=default&platform=desktop

当前回答一共有6200多个,每页5个,那么offset可以有1240页。

二、尝试对某一网页访问

import requests

template = 'https://www.zhihu.com/api/v4/questions/432119474/answers?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled%3B&offset={offset}&limit=5&sort_by=default&platform=desktop'

#for page in range(1, 1240):
    
url = template.format(offset=1)
    
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36'}

resp = requests.get(url, headers=headers)

resp

<Response [200]>

我们注意到5中数据可以展开,大概率是json样式的数据。

在这里插入图片描述

所以尝试使用resp.json()来拿到格式化的字典数据。

在这里插入图片描述

三、解析感兴趣的数据

由于resp.json()返回的是字典,解析定位数据会很方便。

咱们就需求简单点,只采集author、id、excerpt三个字段。

其中author内含有很多更丰富的信息,感兴趣的可以把author再整理下,本文对author不做过多清洗。

for info in resp.json()['data']:
    author = info['author']
    Id = info['id']
    text = info['excerpt']
    data = {'author': author,
            'id': Id,
            'text': text}
    print(data)
{'author': {'id': '10779c1a8b8de0b7f14bb6c217100680', 'url_token': 'da-mo-wang-81-18', 'name': '紧衣卫', 'avatar_url': 'https://pic2.zhimg.com/v2-ebdfa810ec26e81720b8bc14fc6c1d76_l.jpg?source=1940ef5c', 'avatar_url_template': 'https://pic3.zhimg.com/v2-ebdfa810ec26e81720b8bc14fc6c1d76.jpg?source=1940ef5c', 'is_org': False, 'type': 'people', 'url': 'https://www.zhihu.com/api/v4/people/10779c1a8b8de0b7f14bb6c217100680', 'user_type': 'people', 'headline': '梦想太大  心太执着', 'badge': [{'type': 'identity', 'description': '江苏省宝应县公安局 警察', 'topics': []}], 'badge_v2': {'title': '江苏省宝应县公安局 警察', 'merged_badges': [{'type': 'identity', 'detail_type': 'identity', 'title': '认证', 'description': '江苏省宝应县公安局 警察', 'url': 'https://www.zhihu.com/account/verification/intro', 'sources': [], 'icon': '', 'night_icon': ''}], 'detail_badges': [{'type': 'identity', 'detail_type': 'identity_people', 'title': '已认证的个人', 'description': '江苏省宝应县公安局 警察', 'url': 'https://www.zhihu.com/account/verification/intro', 'sources': [], 'icon': 'https://pic1.zhimg.com/v2-235258cecb8a0f184c4d38483cd6f6b6_l.png', 'night_icon': 'https://pic3.zhimg.com/v2-45e870b8f0982bcd7537ea4627afbd00_l.png'}], 'icon': 'https://pic4.zhimg.com/v2-235258cecb8a0f184c4d38483cd6f6b6_l.png', 'night_icon': 'https://pic3.zhimg.com/v2-45e870b8f0982bcd7537ea4627afbd00_l.png'}, 'gender': 1, 'is_advertiser': False, 'follower_count': 3914, 'is_followed': False, 'is_privacy': False}, 'id': 1597225705, 'text': '我也不喜欢过洋节,但我知道最起码的人情世故。俗话说出手不打笑脸人 阿姨是给你送巧克力,又不是给你送鹤顶红 如果内心不愿意接受就婉拒好了 这个有什么好举报的?即使阿姨真的是为了庆祝感恩节 你又想得到一个什么样的举报结果呢?据说举报人是个博士生,我看也别研究别的了,转方向先学习学习最基本的为人处事方法吧。'}
{'author': {'id': '09c8a026f91155c87188ee6e45301f05', 'url_token': 'cao-ling-er-36', 'name': '曹小灵', 'avatar_url': 'https://pic1.zhimg.com/v2-942365af26ff3abd44f6c75c9899306e_l.jpg?source=1940ef5c', 'avatar_url_template': 'https://pic2.zhimg.com/v2-942365af26ff3abd44f6c75c9899306e.jpg?source=1940ef5c', 'is_org': False, 'type': 'people', 'url': 'https://www.zhihu.com/api/v4/people/09c8a026f91155c87188ee6e45301f05', 'user_type': 'people', 'headline': '伪商业分析*抽风式咨询*个人成长', 'badge': [{'type': 'identity', 'description': 'Backbase 管理分析咨询师', 'topics': []}], 'badge_v2': {'title': 'Backbase 管理分析咨询师', 'merged_badges': [{'type': 'identity', 'detail_type': 'identity', 'title': '认证', 'description': 'Backbase 管理分析咨询师', 'url': 'https://www.zhihu.com/account/verification/intro', 'sources': [], 'icon': '', 'night_icon': ''}], 'detail_badges': [{'type': 'identity', 'detail_type': 'identity_people', 'title': '已认证的个人', 'description': 'Backbase 管理分析咨询师', 'url': 'https://www.zhihu.com/account/verification/intro', 'sources': [], 'icon': 'https://pic1.zhimg.com/v2-235258cecb8a0f184c4d38483cd6f6b6_l.png', 'night_icon': 'https://pic1.zhimg.com/v2-45e870b8f0982bcd7537ea4627afbd00_l.png'}], 'icon': 'https://pic2.zhimg.com/v2-235258cecb8a0f184c4d38483cd6f6b6_l.png', 'night_icon': 'https://pic2.zhimg.com/v2-45e870b8f0982bcd7537ea4627afbd00_l.png'}, 'gender': 1, 'is_advertiser': False, 'follower_count': 412637, 'is_followed': False, 'is_privacy': False}, 'id': 1597380398, 'text': '亲人们,知道为什么很多机关办事人员会僵化么?知道为什么很多机构不愿意创新么?知道为什么很多组织都是照章办事缺乏灵活么?你看到的这个案例,就是原因。很多事情,你不做,就不会错;做了,取得的回报很少,但是承担的风险很高。你永远不知道什么地方会突然飞过来一泡口水,把你的好意扭曲为恶意,把你的行为定性为「不妥」。如果是在影视行业,不断在边缘疯狂的试探,还可以理解,这样可以给观众带来新的刺激,换来一句…'}
{'author': {'id': 'a82cf4ad4be905dbc044297e70e3b382', 'url_token': 'alyssa-song', 'name': '貂丝女婶教授', 'avatar_url': 'https://pic4.zhimg.com/v2-2b1a52a861aecec566a3c2c4f00a5e06_l.jpg?source=1940ef5c', 'avatar_url_template': 'https://pic2.zhimg.com/v2-2b1a52a861aecec566a3c2c4f00a5e06.jpg?source=1940ef5c', 'is_org': False, 'type': 'people', 'url': 'https://www.zhihu.com/api/v4/people/a82cf4ad4be905dbc044297e70e3b382', 'user_type': 'people', 'headline': '985副教授,少女型大妈,生化环材', 'badge': [], 'badge_v2': {'title': '', 'merged_badges': [], 'detail_badges': [], 'icon': '', 'night_icon': ''}, 'gender': 0, 'is_advertiser': False, 'follower_count': 10200, 'is_followed': False, 'is_privacy': False}, 'id': 1597237844, 'text': '我从前就住在哈工大的17公寓。宿管阿姨们真的非常好,非常温暖。我当时有一段生病天天待在寝室,阿姨们白天巡寝的时候还经常问我好点了没有,用不用帮我从楼下带盒饭上来。对我这种从小就缺爱的人,阿姨真的就像自己的亲人一样,让我从内心深处感觉到了家的温暖。住在17公寓的日子真的超级美好。当年哈工大的后勤管理还有一点很好,就是每一届毕业的学生把不穿也不带走的旧大衣捐给学校,新一届本科生入校时候,贫困生可以每人…'}
{'author': {'id': 'e6bbeff00614e94be5225c9fb401f989', 'url_token': 'zai-yi-qi-61-23', 'name': '精分的曹小灵', 'avatar_url': 'https://pic4.zhimg.com/v2-d3325d2f1d7de8511a5e9a328c75a539_l.jpg?source=1940ef5c', 'avatar_url_template': 'https://pic2.zhimg.com/v2-d3325d2f1d7de8511a5e9a328c75a539.jpg?source=1940ef5c', 'is_org': False, 'type': 'people', 'url': 'https://www.zhihu.com/api/v4/people/e6bbeff00614e94be5225c9fb401f989', 'user_type': 'people', 'headline': '头脑中存在两种截然相反的思想,开始精分', 'badge': [], 'badge_v2': {'title': '', 'merged_badges': [], 'detail_badges': [], 'icon': '', 'night_icon': ''}, 'gender': 1, 'is_advertiser': False, 'follower_count': 4997, 'is_followed': False, 'is_privacy': False}, 'id': 1597348175, 'text': '啊…又来了。对这种道德高地站的稳的学生,张博洋曾经发出过灵魂质问:[图片] [图片] 宿管阿姨明确的说了:这是西方的节日,自己是借着这个节日的机会,感谢同学们对工作的支持。现在很多女生找各种借口过节,很多男生找各种借口送礼,怎么就不能让宿管阿姨给你一个不想奋斗的机会呢?这种人,不配当个男人,至少不配当一个新时代的以阿姨包养为梦想的好男人。你背叛了你的阶级!手动差评!要上纲上线,别急,咱们一层一层的来。先讲历…'}
{'author': {'id': '0ba346aab1e8761d12e8d1cf28fce8f8', 'url_token': 'xia-xiao-qi-61', 'name': '瞄小七.sunshine', 'a

四、存储到csv

设置三个字段,存储到csv。

注意现在只是对某一页面的数据进行的存储。

import csv

csvf = open('zhihu.csv', 'a+', encoding='utf-8', newline='')
fieldnames = ['author', 'id', 'text']
writer = csv.DictWriter(csvf, fieldnames=fieldnames)
writer.writeheader()

for info in resp.json()['data']:
    author = info['author']
    Id = info['id']
    text = info['excerpt']
    data = {'author': author,
            'id': Id,
            'text': text}
    writer.writerow(data)
    
csvf.close()

五、整理汇总代码

将1234代码合并整理,就是一个对知乎问题回答的批量采集爬虫。

注意复制粘贴代码,要注意层次关系,上下变量名要对应衔接。

import requests
import csv
import time
#新建csv,存储数据
csvf = open('zhihu.csv', 'a+', encoding='utf-8', newline='')
fieldnames = ['author', 'id', 'text']
writer = csv.DictWriter(csvf, fieldnames=fieldnames)
writer.writeheader()
#伪装头,用于反爬
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36'}

#网址模板
template = 'https://www.zhihu.com/api/v4/questions/432119474/answers?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled%3B&offset={offset}&limit=5&sort_by=default&platform=desktop'

#批量采集,
#目前可爬1240+页数据
#这里仅抓取前100页
for page in range(1, 100):
    #对第page页进行访问
    url = template.format(offset=page)
    resp = requests.get(url, headers=headers)
    
    #解析定位第page页的数据
    for info in resp.json()['data']:
        author = info['author']
        Id = info['id']
        text = info['excerpt']
        data = {'author': author,
                'id': Id,
                'text': text}
        #存入csv
        writer.writerow(data)
        
    #降低爬虫对知乎的访问速度
    time.sleep(1) 
    
#关闭csvf
csvf.close()

测试

采集完毕后,我们检查下zhihu.csv,看看是否采集到的数据。

import pandas as pd

df = pd.read_csv('zhihu.csv')
df.head()

在这里插入图片描述

词云图

选中所有回答的内容text,制作词云图,可以看到知乎用户对哈工大感恩节事件的看法。

import jieba

import json

from pyecharts import options as opts
from pyecharts.charts import WordCloud

text_contents = ''.join(df['text'])
words = jieba.lcut(text_contents)
words = [w for w in words if len(w)>1]
wordfreqs = [(w, words.count(w)) for w in set(words)]

c = (
    WordCloud()
    .add("", 
         wordfreqs, 
         word_size_range=[10, 70])
    .set_global_opts(title_opts=opts.TitleOpts(title="哈工大感恩节"))
    .render_notebook()
)

c

在这里插入图片描述


Python学习资源分享

如果你对Python感兴趣,学好 Python 不论是就业、副业赚钱、还是提升学习、工作效率,都是非常不错的选择,但要有一个系统的学习规划。

小编是一名Python开发工程师,自己整理了一套 【最新的Python系统学习教程】,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。

如果你是准备学习Python或者正在学习,下面这些你应该能用得上:

1、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

在这里插入图片描述

2、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

在这里插入图片描述

3、精品书籍

书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

在这里插入图片描述

4、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

img

5、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

img

6、清华编程大佬出品《漫画看学Python》

用通俗易懂的漫画,来教你学习Python,让你更容易记住,并且不会枯燥乏味。

在这里插入图片描述

7、Python副业兼职与全职路线

在这里插入图片描述

这份完整版的Python全套学习资料已经上传至CSDN官方,朋友们如果需要可以点击下方链接扫描下方二v码都可以免费获取【保证100%免费】

最新全套【Python入门到进阶资料 & 实战源码 &安装工具】

以上全套资料已经为大家打包准备好了,希望对正在学习Python的你有所帮助!

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值