深圳有什么好的企业网站,element ui做的网站,做网站接活全流程,东莞网站设计制作网站1. 知乎文章图片爬取器之一写在前面
今天开始尝试爬取一下知乎#xff0c;看一下这个网站都有什么好玩的内容可以爬取到#xff0c;可能断断续续会写几篇文章#xff0c;今天首先爬取最简单的#xff0c;单一文章的所有回答#xff0c;爬取这个没有什么难度。
找到我们要爬…1. 知乎文章图片爬取器之一写在前面
今天开始尝试爬取一下知乎看一下这个网站都有什么好玩的内容可以爬取到可能断断续续会写几篇文章今天首先爬取最简单的单一文章的所有回答爬取这个没有什么难度。
找到我们要爬取的页面我随便选了一个
https://www.zhihu.com/question/2923939471084个回答数据量可以说非常小了就爬取它吧。
2. 知乎文章图片爬取器之一选取操作库和爬取地址
爬取使用requests 存储使用 mongodb 就可以了
爬取地址经过分析之后找到了一个可以返回json的数据接口提取链接看一下各参数的意思方便我们程序模拟
https://www.zhihu.com/api/v4/questions/292393947/answers?includedata%5B%2A%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%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%2A%5D.topicslimit5offset10sort_bydefault
上面的连接进行了URL编码去找个解码工具解析一下编程下面的URL就比较好解释了answers后面跟了一堆的参数应该是返回的关键字找到limit每页显示的数据量offset偏移量我们下拉滚动条发现这个在不断的叠加5sort_by 就是排序。
https://www.zhihu.com/api/v4/questions/292393947/answers?includedata[*].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,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topicslimit5offset10sort_bydefault
做好上面的工作接下来就是爬取了我简化了一下爬取的地址只保留了一些关键的信息
https://www.zhihu.com/api/v4/questions/292393947/answers?includecomment_count,content,voteup_count,reshipment_settings,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topicslimit5offset0sort_bydefault
3. 知乎文章图片爬取器之一编写代码
分析完毕之后发现代码非常简单了
import requests
from fake_useragent import UserAgent
############## 数据存储
import pymongo
import time
DATABASE_IP 127.0.0.1
DATABASE_PORT 27017
DATABASE_NAME sun
client pymongo.MongoClient(DATABASE_IP,DATABASE_PORT)
db client.sun
db.authenticate(dba, dba)
collection db.zhihuone # 准备插入数据
##################################
class ZhihuOne(object):
def __init__(self,totle):
self._offset 0
self._totle totle
#self._ua UserAgent()
def run(self):
print(正在抓取 {} 数据.format(self._offset))
headers {
upgrade-insecure-requests:1,
user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64)
}
with requests.Session() as s:
try:
with s.get(https://www.zhihu.com/api/v4/questions/292393947/answers?includecomment_count,content,voteup_count,reshipment_settings,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topicslimit5offset{}sort_bydefault.format(self._offset),headersheaders,timeout3) as rep:
data rep.json()
if data:
collection.insert_many(data[data])
except Exception as e:
print(e.args)
finally:
if self._offset self._totle:
self._offset self._offset 5 # 每次5
print(防止被办休息3s)
time.sleep(3)
self.run()
else:
print(所有数据获取完毕)
if __name__ __main__:
# 偏移量是0,5,10 i1 (i-1)*5
zhi ZhihuOne(1084)
zhi.run()
上面主程序入口中我写了个1084 这个偷懒就硬编码了数据当然也可以通过爬取获取没有任何问题
4. 知乎文章图片爬取器之一写在后面
本篇文章是知乎文章爬取器之一接下来完善的功能 1. 爬取地址用户可以输入 2. 自动答案总数 3. 文章中图片自动下载 4. 等功能