1. 程式人生 > >Python模擬登陸新版知乎

Python模擬登陸新版知乎

目前網上很多模擬登入知乎的程式碼已經無法使用,即使是二、三月的程式碼也已經無法模擬登陸知乎,所以我現在將新版知乎的模擬登入程式碼和講解發布出來。

零、開發環境

  1. 開發工具:Pycharm
  2. Python版本:3.6
  3. 執行環境:Win10

一、程式碼和講解

# 利用requests 模擬登陸
import requests
import http.cookiejar as cookielib
import re
import time
import hmac
from hashlib import sha1
import json
import base64
from PIL import
Image # 利用session保持連結 session = requests.session() session.cookies = cookielib.LWPCookieJar(filename="cookies.txt") # cookie儲存檔案, # 提取儲存的cookie try: session.cookies.load(ignore_discard=True) # 從檔案中讀取cookie except: print("cookie 未能載入") # 偽造header agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
header = { "HOST": "www.zhihu.com", "Referer": "https://www.zhihu.com", "User-Agent": agent, 'Connection': 'keep-alive' } def is_login(): # 通過個人中心頁面返回狀態碼來判斷是否登入 # 通過allow_redirects 設定為不獲取重定向後的頁面 response = session.get("https://www.zhihu.com/inbox", headers=header, allow_redirects=False
) if response.status_code != 200: zhihu_login("+8618511693445", "123*asd") else: print("你已經登陸了") def get_xsrf_dc0(): # 獲取xsrf code和d_c0 # 在請求登入頁面的時候頁面會將xsrf code 和d_c0加入到cookie中返回給客戶端 response = session.get("https://www.zhihu.com/signup", headers=header) return response.cookies["_xsrf"], response.cookies["d_c0"] def get_signature(time_str): # 生成signature,利用hmac加密 # 根據分析之後的js,可發現裡面有一段是進行hmac加密的 # 分析執行加密的js 程式碼,可得出加密的欄位,利用python 進行hmac幾碼 h = hmac.new(key='d1b964811afb40118a12068ff74a12f4'.encode('utf-8'), digestmod=sha1) grant_type = 'password' client_id = 'c3cef7c66a1843f8b3a9e6a1e3160e20' source = 'com.zhihu.web' now = time_str h.update((grant_type + client_id + source + now).encode('utf-8')) return h.hexdigest() def get_identifying_code(headers): # 判斷頁面是否需要填寫驗證碼 # 如果需要填寫則彈出驗證碼,進行手動填寫 # 請求驗證碼的url 後的引數lang=en,意思是取得英文驗證碼 # 原因是知乎的驗證碼分為中文和英文兩種 # 中文驗證碼是通過選擇倒置的漢字驗證的,破解起來相對來說比較困難, # 英文的驗證碼則是輸入驗證碼內容即可,破解起來相對簡單,因此使用英文驗證碼 response = session.get('https://www.zhihu.com/api/v3/oauth/captcha?lang=en', headers=headers) # 盤但是否存在驗證碼 r = re.findall('"show_captcha":(\w+)', response.text) if r[0] == 'false': return '' else: response = session.put('https://www.zhihu.com/api/v3/oauth/captcha?lang=en', headers=header) show_captcha = json.loads(response.text)['img_base64'] with open('captcha.jpg', 'wb') as f: f.write(base64.b64decode(show_captcha)) im = Image.open('captcha.jpg') im.show() im.close() captcha = input('輸入驗證碼:') session.post('https://www.zhihu.com/api/v3/oauth/captcha?lang=en', headers=header, data={"input_text": captcha}) return captcha def zhihu_login(account, password): '''知乎登陸''' post_url = 'https://www.zhihu.com/api/v3/oauth/sign_in' XXsrftoken, XUDID = get_xsrf_dc0() header.update({ "authorization": "oauth c3cef7c66a1843f8b3a9e6a1e3160e20", # 固定值 "X-Xsrftoken": XXsrftoken, }) time_str = str(int((time.time() * 1000))) # 直接寫在引號內的值為固定值, # 只要知乎不改版反爬蟲措施,這些值都不湖邊 post_data = { "client_id": "c3cef7c66a1843f8b3a9e6a1e3160e20", "grant_type": "password", "timestamp": time_str, "source": "com.zhihu.web", "password": password, "username": account, "captcha": "", "lang": "en", "ref_source": "homepage", "utm_source": "", "signature": get_signature(time_str), 'captcha': get_identifying_code(header) } response = session.post(post_url, data=post_data, headers=header, cookies=session.cookies) if response.status_code == 201: # 儲存cookie,下次直接讀取儲存的cookie,不用再次登入 session.cookies.save() else: print("登入失敗") if __name__ == '__main__': is_login()

加入QQ群,共享IT技術和資源
這裡寫圖片描述