Python爬去知乎上问题下所有图片
Posted 守拙的瘦子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python爬去知乎上问题下所有图片相关的知识,希望对你有一定的参考价值。
from zhihu_oauth import ZhihuClient from zhihu_oauth.exception import NeedCaptchaException client = ZhihuClient() try: client.login(‘email_or_phone‘, ‘password‘) print(u"登陆成功!") except NeedCaptchaException: # 保存验证码并提示输入,重新登录 with open(‘a.gif‘, ‘wb‘) as f: f.write(client.get_captcha()) captcha = input(‘please input captcha:‘) client.login(‘+8613872273541‘, ‘z289784552‘, captcha) print(u"登陆成功!") client.save_token(‘token.pkl‘)
from __future__ import print_function # 使用python3的print方法 from zhihu_oauth import ZhihuClient import re import os import urllib.request client = ZhihuClient() # 登录 client.load_token(‘token.pkl‘) # 加载token文件 id = 46508954 # https://www.zhihu.com/question/24400664(长得好看是一种怎么样的体验) question = client.question(id) print(u"问题:",question.title) print(u"回答数量:",question.answer_count) os.mkdir(question.title + u"(图片)") path = question.title + u"(图片)" index = 1 # 图片序号 for answer in question.answers: content = answer.content # 回答内容 re_compile = re.compile(r‘<img src="(https://picd.zhimg.com/.*?.(jpg|png))".*?>‘) img_lists = re.findall(re_compile, content) if (img_lists): for img in img_lists: img_url = img[0] # 图片url urllib.request.urlretrieve(img_url, path + u"/%d.jpg" % index) print(u"成功保存第%d张图片" % index) index += 1
以上是关于Python爬去知乎上问题下所有图片的主要内容,如果未能解决你的问题,请参考以下文章