DecryptLogin:python模拟登陆模块

2022/11/2 23:24:52

本文主要是介绍DecryptLogin:python模拟登陆模块,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

DecryptLogin:python模拟登陆模块

一、简单登陆
infos_return:返回一个字典对象,包含用户信息 session:会话对象 所有网站接口都包含以下参数: username: 登录用户名 password: 登录密码 mode:移动端:mobile’和PC端:mode=‘pc’ crackvcFunc: 支持用户自定义一个验证码识别函数, 该函数传入验证码图片路径, 并返回识别结果 proxies: 模拟登录的过程中使用指定的代理服务器, 代理支持的格式同: https://requests.readthedocs.io/en/master/user/advanced/#proxies

二、验证码处理
需要用到python图像处理库——PIL 并定义验证码识别函数。

def crackvcFunc(imagepath):
    # 打开验证码图片
    img = Image.open(imagepath)
    # 识别验证码图片
    result = IdentifyAPI(img)
    # 返回识别结果
    return result
其中IdentifyAPI可选择识别验证码的接口,我选用的是百度ai通用文字识别的接口,,修改后的代码如下:

def get_access_token(client_id,client_secret):
    host = https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=+ client_id + &client_secret= + client_secret
    response = requests.get(host)
    res = response.json()
    if response:
        return res[access_token]

def crackvcFunc(imagepath):
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
    # 二进制方式打开图片文件
    f = open(imagepath, rb)
    img = base64.b64encode(f.read())
    params = {
          
   "image":img}
    access_token = get_access_token(client_id,client_secret)
    request_url = request_url + "?access_token=" + access_token
    headers = {
          
   content-type: application/x-www-form-urlencoded}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        res = response.json()
        return res[words_result][0][words]
登陆成功

[INFO]: Account -> 18**********5, login successfully...
[Finished in 5.1s]
三、添加代理
proxies = {
          
   https: 127.0.0.1:1080}
infos_return, session = lg.zhihu(username=Your Username, password=Your Password, proxies=proxies)
四、保存cookies
为安全起见, DecryptLogin不考虑提供自动保存cookies并每次验证其是否已经过期的功能。

from DecryptLogin.utils.cookies import *

def save_cookies(url, cookiespath):
    session = requests.Session()
    for i in url:
        session.get(i)
        saveSessionCookies(session=session, cookiespath=cookiespath)
五、载入cookies
def load_cookies(cookiespath):
    session = requests.Session()
    infos_return, session = loadSessionCookies(session=session, cookiespath=cookiespath)
    return infos_return, session
最后主函数:

def main():
    lg = login.Login()#登陆
    client_id = GoH*******CtNy #验证码识别API接口id
    client_secret = xCgB*********cytKDCK #验证码识别API接口密码
    cookiespath = c:/****#cookies保存路径
    username = username
    password = password
    infos_return, session = lg.weibo(username, password, pc) #登陆
    save_cookies(infos_return[crossDomainUrlList], cookiespath)
    infos_return, session = load_cookies(cookiespath)

这篇关于DecryptLogin:python模拟登陆模块的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程