点击上方 “蓝字” 关注,学习生活两不误
点击上方“python学习专栏”,选择“置顶公众号”
干货、福利第一时间送达!
文末有 word版获取方式
日常刷知乎,我是一个爱学习的人
今天让我看到了一条,有哪些比较新的土味情话?
对于钢铁直男而言,这是新大陆啊
学起来
乍一看,很多大佬回答了,还挺多。
于是乎,我决定去将这些土味情话保存下来
以备不时之需
所以,这篇文章就面世,收集 土味情话
当然了,对于一个学习python的我,就算是爬虫技术一般般
对于爬取知乎的评论还是能够轻松做到的
一步一步
1、分析知乎页面
2、分析链接
3、获取评论
4、保存到word文档
1、分析知乎页面
给出链接 https://www.zhihu.com/question/293527229
我们直接使用浏览器自带的 抓包工具
点击F12,或者右键 点击 审查元素(360浏览器)
然后刷新一下页面,按照下图依次点击
点击到3 时刷新一下页面,然后往下拉网页页面,
知道4所示字段出现,点击,点击5, 复制6 所示的链接
2、分析链接
由于 知乎采用的是动态返回数据
我们不可能由一个链接来得到我们要的全部数据
再次滑动页面获取一个 和上面字段一样的header中的链接
看看两个链接的不同之处,就是关注limit 和 offset ,这个的意思是
每次获取5条评论(limit),offset 是现在获取到多少条
也就是 第一次 是 0-5,第二次是6-10
url = 'https://www.zhihu.com/api/v4/questions/293527229/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%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&offset= {}&limit=5&sort_by=default&platform=desktop'.format(str(i))
只要在这里对 offset进行改变即可
3、获取评论
使用我们获取的链接打开,获取的是json 数据
这里我们需要使用 json.loads 转化为python的数据类型,字典
import requestsimport jsonresponse = requests.get(url=url, headers=headers)res = json.loads(response.content.decode())soup = BeautifulSoup(res['data'][0]['content'], 'lxml')
res['data'][0]['content'] 这里面就是评论的内容
这些数据是以 网页代码形式存储的
我们需要转为html ,然后获取内容
soup = BeautifulSoup(res['data'][0]['content'], 'lxml')con = soup.select('p') #获取p标签con.get_text() #获取文本
4、存储为word文档
这里需要使用到 docx库
pip install python-docx #安装
from docx import Documentfrom docx.enum.text import WD_ALIGN_PARAGRAPHdocument = Document() #创建 word处理document.add_heading('Document Title', 0) #插入标题p = document.add_paragraph('123456789\n') #插入段落p.alignment = WD_ALIGN_PARAGRAPH.CENTER #居中对齐
想要更多的了解这个库,可以到官网学习
https://python-docx.readthedocs.io/en/latest/index.html
完整代码
def request_data(num):headers = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8','user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',}print('回答数量=', num)document = Document()document.add_heading('土味情话', 0) #插入标题document.add_heading('来源于知乎', 2) #插入标题for i in range(0,num,5):print('请求了 {} 回答'.format(i))url = 'https://www.zhihu.com/api/v4/questions/293527229/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%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&offset={}&limit=5&sort_by=default&platform=desktop'.format(str(i))response = requests.get(url=url, headers=headers)res = json.loads(response.content.decode())for content in res['data']:document.add_heading('作者:{},点赞人数:{}'.format(content['author']['name'], content['voteup_count']), 0) #插入标题soup = BeautifulSoup(content['content'], 'lxml')#大致看了一下,评论的内容有两类标签if len(soup.select('blockquote')) > 5: #回答小于5 则过滤掉for text in soup.select('blockquote'):pare = document.add_paragraph(text.get_text()) #插入段落pare.alignment = WD_ALIGN_PARAGRAPH.CENTER #中间对齐elif len(soup.select('p')):for text in soup.select('p'):pare = document.add_paragraph(text.get_text())pare.alignment = WD_ALIGN_PARAGRAPH.CENTERdocument.save('土味情话3.docx')if __name__ == '__main__':num = int(input('输入想获取的数量')) #这里是指评论的数量,并不是情话数量request_data(260)
看完了,记得点在看
关注公众号
回复:土味情话
即可获取,5000句word完整版土味情话
end
长按识别二维码关注