咱们以爬取“为什么中医莫得取得外界认同?”为例来商讨一下若何爬取知乎中问题的恢复以及驳倒的数据美女被张开双腿日出白浆。
图片
爬取网页数据频繁情况下会资格以下三个门径。
第一步:网页分析,说明我方所要数据的真确存储地址,以及这些url地址的规章。
第二步:爬取网页数据,并对这些数据进行清洗和整理形成结构化数据。
第三步:存储数据美女被张开双腿日出白浆,以便于背面的分析。
底下咱们差别来预防先容。
一、网页分析咱们应用Chrome浏览器,翻开所要爬取的网页:
https://www.zhihu.com/question/370697253
按F12检察元素,点击“Network”,再点击“XHR”选项。
先按左边的小圆圈清空列表,浅显背面查找申请积贮,再按“F5”刷新一下网页美女被张开双腿日出白浆,如下图所示:
图片
在列表中找到存储恢复数据的url地址,点击后在“Preview”面板不错看到Josn款式的数据。
知悉每一页数据对应的url地址。
第1页:
https://www.zhihu.com/api/v4/questions/370697253/answers?include=data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,attachment,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,is_labeled,paid_info,paid_info_content,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp,is_recognized;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics;data[*].settings.table_of_content.enabled&limit=5&offset=0&platform=desktop&sort_by=default第2页:美女被张开双腿日出白浆
https://www.zhihu.com/api/v4/questions/370697253/answers?include=data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,attachment,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,is_labeled,paid_info,paid_info_content,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp,is_recognized;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics;data[*].settings.table_of_content.enabled&limit=5&offset=5&platform=desktop&sort_by=default第3页:美女被张开双腿日出白浆
https://www.zhihu.com/api/v4/questions/370697253/answers?include=data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,一本色道久久综合亚洲精品editable_content,attachment,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,is_labeled,paid_info,paid_info_content,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp,is_recognized;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics;data[*].settings.table_of_content.enabled&limit=5&offset=10&platform=desktop&sort_by=default咱们发现,除了offset属性对应的取值不同,其余部分一路同样。而况offset属性对应的取值从0启动,每一页收支5。终末一页Json中的 paging -> is_end属性为false。
以上是问题恢复的网页分析。咱们再分析一下针对每个恢复的驳倒。
图片
跟上头的门径同样美女被张开双腿日出白浆,找到这些驳倒存储的真确蚁合地址。
图片
知悉每一页数据对应的url地址如下:
第1页:
https://www.zhihu.com/api/v4/answers/1014424784/root_comments?order=normal&limit=20&offset=0&status=open第2页:
https://www.zhihu.com/api/v4/answers/1014424784/root_comments?order=normal&limit=20&offset=20&status=open第3页:
https://www.zhihu.com/api/v4/answers/1014424784/root_comments?order=normal&limit=20&offset=40&status=open“1014424784”是该恢复的id,不同的恢复该id值不同。上头的url是针对合并恢复的驳倒,这些url地址除了offset属性对应的取值不同,其余部分一路同样。而况offset属性对应的取值从0启动,每一页收支20。终末一页Json中的 paging -> is_end属性为false。
二、常用库先容(1)requestsrequests的作用便是发送蚁合申请,复返反馈数据。
官方文档如下:
https://docs.python-requests.org/zh_CN/latest/user/quickstart.html
(2)jsonJson 是一种轻量级的数据交换款式美女被张开双腿日出白浆,系数孤独于任何圭臬话语的文本款式。一般,后台应用圭臬将反馈数据封装成Json款式复返。
官方文档如下:
https://docs.python.org/zh-cn/3.7/library/json.html
(3)lxmllxml 是一个HTML/XML的分解器,主邀功能是分解和索取 HTML/XML 数据。
如果你也经常关注视频号,肯定也已经发现,视频号除了点赞、评论、收藏、转发外,并没有下载功能。
接着点击【微信支付】;美女被张开双腿日出白浆
图片
官方文档如下:
https://lxml.de/index.html
由于本图文的篇幅有限,背面会另写图文差别先容上头这些跟爬虫关系的库。
三、圆善代码GetAnswers措施用于爬取问题的恢复数据。
恢复数据结构化之后的属性有:帖子的ID(answer_id)、作家称呼(author)、发表本事(created_time)、帖子骨子(content)。
GetComments措施用于爬取每个问题的驳倒数据。
驳倒数据结构化之后的属性有:驳倒的ID(answer_id_comment_id)、作家称呼(author)、发表本事(created_time)、驳倒骨子(content)。
这些数据一路存储在“知乎驳倒.csv”这个文献中,需要提神的是该文献用Excel翻开之后出现汉文乱码美女被张开双腿日出白浆,惩办措施不错参考前边的一篇图文若何惩办Python3写入CSV出现'gbk' codec can't encode的失实?
import requestsimport jsonimport timeimport csvfrom lxml import etreeheaders = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36',}csvfile = open('知乎驳倒.csv', 'w', newline='', encoding='utf-8')writer = csv.writer(csvfile)writer.writerow(['id', 'created_time', 'author', 'content'])def GetAnswers(): i = 0 while True: url = 'https://www.zhihu.com/api/v4/questions/370697253/answers' \ '?include=data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action%' \ '2Cannotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count%' \ '2Ccan_comment,content,editable_content,attachment,voteup_count,reshipment_settings%' \ '2Ccomment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt%' \ '2Cis_labeled,paid_info,paid_info_content,relationship.is_authorized,is_author,voting,is_thanked%' \ '2Cis_nothelp,is_recognized;data[*].mark_infos[*].url;data[*].author.follower_count%' \ '2Cbadge[*].topics;data[*].settings.table_of_content.enabled&limit=5&offset={0}&platform=desktop&' \ 'sort_by=default'.format(i) state=1 while state: try: res = requests.get(url, headers=headers, timeout=(3, 7)) state=0 except: continue res.encoding = 'utf-8' jsonAnswer = json.loads(res.text) is_end = jsonAnswer['paging']['is_end'] for data in jsonAnswer['data']: l = list() answer_id = str(data['id']) l.append(answer_id) l.append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data['created_time']))) l.append(data['author']['name']) l.append(''.join(etree.HTML(data['content']).xpath('//p//text()'))) writer.writerow(l) print(l) if data['admin_closed_comment'] == False and data['can_comment']['status'] and data['comment_count'] > 0: GetComments(answer_id) i += 5 print('打印到第{0}页'.format(int(i / 5))) if is_end: break time.sleep(1)def GetComments(answer_id): j = 0 while True: url = 'https://www.zhihu.com/api/v4/answers/{0}/root_comments?order=normal&limit=20&offset={1}&status=open'.format( answer_id, j) state=1 while state: try: res = requests.get(url, headers=headers, timeout=(3, 7)) state=0 except: continue res.encoding = 'utf-8' jsonComment = json.loads(res.text) is_end = jsonComment['paging']['is_end'] for data in jsonComment['data']: l = list() comment_id = str(answer_id) + "_" + str(data['id']) l.append(comment_id) l.append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data['created_time']))) l.append(data['author']['member']['name']) l.append(''.join(etree.HTML(data['content']).xpath('//p//text()'))) writer.writerow(l) print(l) for child_comments in data['child_comments']: l.clear() l.append(str(comment_id) + "_" + str(child_comments['id'])) l.append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(child_comments['created_time']))) l.append(child_comments['author']['member']['name']) l.append(''.join(etree.HTML(child_comments['content']).xpath('//p//text()'))) writer.writerow(l) print(l) j += 20 if is_end: break time.sleep(1)GetAnswers()csvfile.close()四、追溯 本站是提供个人学问治理的蚁合存储空间,通盘骨子均由用户发布,不代表本站意见。请提神甄别骨子中的预料容貌、教化购买等信息,提神骗取。如发现存害或侵权骨子,请点击一键举报。