python爬虫知乎图片_Python爬虫入门教程 25-100 知乎文章图片爬取器之一
1. 知乎文章圖片爬取器之一寫(xiě)在前面
今天開(kāi)始嘗試爬取一下知乎,看一下這個(gè)網(wǎng)站都有什么好玩的內(nèi)容可以爬取到,可能斷斷續(xù)續(xù)會(huì)寫(xiě)幾篇文章,今天首先爬取最簡(jiǎn)單的,單一文章的所有回答,爬取這個(gè)沒(méi)有什么難度。
找到我們要爬取的頁(yè)面,我隨便選了一個(gè)
https://www.zhihu.com/question/292393947
1084個(gè)回答,數(shù)據(jù)量可以說(shuō)非常小了,就爬取它吧。
2. 知乎文章圖片爬取器之一選取操作庫(kù)和爬取地址
爬取使用requests 存儲(chǔ)使用 mongodb 就可以了
爬取地址經(jīng)過(guò)分析之后,找到了一個(gè)可以返回json的數(shù)據(jù)接口
提取鏈接,看一下各參數(shù)的意思,方便我們程序模擬
https://www.zhihu.com/api/v4/questions/292393947/answers?include=data%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.topics&limit=5&offset=10&sort_by=default
上面的連接進(jìn)行了URL編碼,去找個(gè)解碼工具解析一下,編程下面的URL就比較好解釋了,answers后面跟了一堆的參數(shù),應(yīng)該是返回的關(guān)鍵字,找到limit每頁(yè)顯示的數(shù)據(jù)量,offset偏移量,我們下拉滾動(dòng)條,發(fā)現(xiàn)這個(gè)在不斷的疊加+5,sort_by 就是排序。
https://www.zhihu.com/api/v4/questions/292393947/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,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[*].topics&limit=5&offset=10&sort_by=default
做好上面的工作,接下來(lái)就是爬取了,我簡(jiǎn)化了一下爬取的地址,只保留了一些關(guān)鍵的信息
https://www.zhihu.com/api/v4/questions/292393947/answers?include=comment_count,content,voteup_count,reshipment_settings,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics&limit=5&offset=0&sort_by=default
3. 知乎文章圖片爬取器之一編寫(xiě)代碼
分析完畢之后,發(fā)現(xiàn)代碼非常簡(jiǎn)單了
import requests
from fake_useragent import UserAgent
############## 數(shù)據(jù)存儲(chǔ)
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 # 準(zhǔn)備插入數(shù)據(jù)
##################################
class ZhihuOne(object):
def __init__(self,totle):
self._offset = 0
self._totle = totle
#self._ua = UserAgent()
def run(self):
print("正在抓取 {} 數(shù)據(jù)".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?include=comment_count,content,voteup_count,reshipment_settings,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics&limit=5&offset={}&sort_by=default".format(self._offset),headers=headers,timeout=3) 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("所有數(shù)據(jù)獲取完畢")
if __name__ == '__main__':
# 偏移量是0,5,10 i=1 (i-1)*5
zhi = ZhihuOne(1084)
zhi.run()
上面主程序入口中,我寫(xiě)了個(gè)1084 ,這個(gè)偷懶,就硬編碼了,數(shù)據(jù)當(dāng)然也可以通過(guò)爬取獲取,沒(méi)有任何問(wèn)題
4. 知乎文章圖片爬取器之一寫(xiě)在后面
本篇文章是知乎文章爬取器之一,接下來(lái)完善的功能 1. 爬取地址用戶可以輸入 2. 自動(dòng)答案總數(shù) 3. 文章中圖片自動(dòng)下載 4. 等功能
總結(jié)
以上是生活随笔為你收集整理的python爬虫知乎图片_Python爬虫入门教程 25-100 知乎文章图片爬取器之一的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 小米pay支持的银行卡/信用卡有哪些
- 下一篇: 国债的流动性怎么样?根据国债类型而定