知乎驗證碼登陸

整體代碼

#!/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')
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容