美文网首页
知乎验证码登陆

知乎验证码登陆

作者: Py_Explorer | 来源:发表于2017-11-13 10:17 被阅读0次

    整体代码

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import sys
    reload(sys)
    sys.setdefaultencoding( "utf-8" )
    import requests
    import time
    import pytesseract
    from PIL import Image
    from bs4 import BeautifulSoup
    
    def captcha(data):
        with open('captcha.jpg','wb') as fp:
            fp.write(data)
        time.sleep(1)
        image = Image.open("captcha.jpg")
        text = pytesseract.image_to_string(image)
        print "机器识别后的验证码为:" + text
        command = raw_input("请输入Y表示同意使用,按其他键自行重新输入:")
        if (command == "Y" or command == "y"):
            return text
        else:
            return raw_input('输入验证码:')
    
    def zhihuLogin(username,password):
    
        # 构建一个保存Cookie值的session对象
        sessiona = requests.Session()
        headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101     Firefox/47.0'}
       # 找到 name 属性值为 _xsrf 的input标签,取出value里的值
       _xsrf = BeautifulSoup(html, 'lxml').find('input', attrs={'name': '_xsrf'}).get('value')
    
       # 取出验证码,r后面的值是Unix时间戳,time.time()
      captcha_url = 'https://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000)
      response = requests.session().post(captcha_url, headers=headers)
      
        data = {
            "_xsrf":_xsrf,
            "email":username,
            "password":password,
            "remember_me":True,
            "captcha": captcha(response.content)
        }
    
        response = sessiona.post('https://www.zhihu.com/login/email', data = data, headers=headers)
        print response.text
    
        response = sessiona.get('https://www.zhihu.com/people/maozhaojun/activities', headers=headers)
        print response.text
    
    
    if __name__ == "__main__":
        #username = raw_input("username")
        #password = raw_input("password")
        # zhihuLogin('xxxx@qq.com','ALAxxxxIME')
        zhihuLogin('15565775195','092744gd')

    相关文章

      网友评论

          本文标题:知乎验证码登陆

          本文链接:https://www.haomeiwen.com/subject/gyiamxtx.html