本篇文章为大家展示了使用Python怎么爬取知乎图片,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
import requests
import re
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 # 准备插入数据
BASE_URL = "https://www.zhihu.com/question/{}"
def get_totle_answers(article_id):
headers = {
"user-agent": "需要自己补全 Mozilla/5.0 (Windows NT 10.0; WOW64)"
}
with requests.Session() as s:
with s.get(BASE_URL.format(article_id),headers=headers,timeout=3) as rep:
html = rep.text
pattern =re.compile( '<meta itemProp="answerCount" content="(\d*?)"/>')
s = pattern.search(html)
print("查找到{}条数据".format(s.groups()[0]))
return s.groups()[0]
if __name__ == '__main__':
# 用死循环判断用户输入的是否是数字
article_id = ""
while not article_id.isdigit():
article_id = input("请输入文章ID:")
totle = get_totle_answers(article_id)
if int(totle)>0:
zhi = ZhihuOne(article_id,totle)
zhi.run()
else:
print("没有任何数据!")
完善图片下载部分,图片下载地址在查阅过程中发现,存在json字段的content中,我们采用简单的正则表达式将他匹配出来。细节如下图展示
编写代码吧,下面的代码注释请仔细阅读,中间有一个小BUG,需要手动把pic3修改为pic2这个地方目前原因不明确,可能是我本地网络的原因,还有请在项目根目录先创建一个imgs的文件夹,用来存储图片
def download_img(self,data):
## 下载图片
for item in data["data"]:
content = item["content"]
pattern = re.compile('<noscript>(.*?)</noscript>')
imgs = pattern.findall(content)
if len(imgs) > 0:
for img in imgs:
match = re.search('<img src="(.*?)"', img)
download = match.groups()[0]
download = download.replace("pic3", "pic2") # 小BUG,pic3的下载不到
print("正在下载{}".format(download), end="")
try:
with requests.Session() as s:
with s.get(download) as img_down:
# 获取文件名称
file = download[download.rindex("/") + 1:]
content = img_down.content
with open("imgs/{}".format(file), "wb+") as f: # 这个地方进行了硬编码
f.write(content)
print("图片下载完成", end="\n")
except Exception as e:
print(e.args)
else:
pass
上述内容就是使用Python怎么爬取知乎图片,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。