使用背景 在自动化登录平台时,某些平台有验证码限制登录。因此开发脚本用来自动识别并提取验证码获得登录权限。
调用库 1 2 3 4 5 import ddddocrimport osimport requestsimport datetimeimport json
抓取图像函数 1 2 3 4 5 6 7 def download_image (self ): url = self .base_url + "/api/app/code/image?width=125&height=33" res = self .session.get(url, headers=self .headers) if res.status_code == 200 : image_path = self .save_image(res=res) Validate_Code_Token = res.headers["Validate-Code-Token" ] return image_path, Validate_Code_Token
保存图像函数 1 2 3 4 5 6 7 8 9 def save_image (self, res ): try : path = self .save_path with open (path, "wb" ) as f: f.write(res.content) print ("图片保存成功" ) return path except requests.exceptions.RequestException as e: print (f"请求出错:{e} " )
验证码识别函数 该函数目前仅支持无干扰图像识别。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 def recognize_captcha (self,img_paths ): """ 识别多个验证码图片 :param img_paths: list[str] 图片路径列表 :return: dict[str, str] 每张图片路径对应的识别结果 """ results = {} for path in img_paths: if not os.path.exists(path): results[path] = "文件不存在" continue try : with open (path, "rb" ) as f: img_data = f.read() text = self .ocr.classification(img_data) results[path] = text except Exception as e: results[path] = f"识别失败: {e} " return results
登录函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 def login (self, max_retries=3 ): url = self .base_url + "/api/app/login" for attempt in range (1 , max_retries + 1 ): print (f"第 {attempt} 次尝试登录..." ) image_path, code_token = self .download_image() if not image_path or not code_token: print ("验证码图片或 token 获取失败,尝试跳过此次登录" ) continue result = self .captcha_recognizer.recognize_captcha([image_path]) code = list (result.values())[0 ] if result else "" print ("验证码为:" + str (code)) if not code or len (code.strip()) == 0 : print ("验证码识别失败,重试中..." ) continue headers = self .headers.copy() headers["validate-code-token" ] = code_token body = { "username" : "" , "password" : "" , "code" : code } try : res = self .session.post(url=url, json=body, headers=headers) res_json = res.json() if res.status_code == 200 and res_json.get("code" ) == "SUCCESS" : token = res_json["data" ]["token" ] print (f"登录成功,token: {token} " ) return token else : print (f"登录失败,响应: {res_json} " ) except Exception as e: print (f"登录请求异常:{e} " ) print ("已达到最大重试次数,登录失败。" )return None
主函数 1 2 login = LoginAPI() token = login.login()
打包错误解决方法 如果提示common_old.onnx file not exits
说明在打包时没有找到common_old.onnx
文件的路径,将common_old.onnx
文件(D:\Program\Python312\Lib\site-packages\ddddocr)
复制到脚本路径下,然后通过终端运行如下指令
1 pyinstaller --onefile --add-data "./common_old.onnx;ddddocr" 脚本名.py
说明:
–add-data ”源路径;目标路径“:告诉Pyinstaller把模型文件一起打包进去。
windows使用;
,linux和macos用:
ddddocr
是模块路径
同理,如果报错缺失common.onnx
文件,则和上述步骤类似。