码迷,mamicode.com
首页 > 编程语言 > 详细

Python爬去知乎上问题下所有图片

时间:2018-08-08 13:33:35      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:pil   color   port   except   读取数据   成功   怎么   hone   url   

技术分享图片
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)
获取Token
技术分享图片
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://pic\d\.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
加载Token并读取数据

 

Python爬去知乎上问题下所有图片

标签:pil   color   port   except   读取数据   成功   怎么   hone   url   

原文地址:https://www.cnblogs.com/wuyujie/p/9441927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!