首页 > 其他分享 >使用 Dart 实现验证码识别与自动化登录

使用 Dart 实现验证码识别与自动化登录

时间:2024-10-17 13:31:54浏览次数:1  
标签:http String 登录 验证码 dart captcha Dart final

  1. 安装所需依赖
    我们将使用以下依赖:

http:用于发送 HTTP 请求,下载验证码图片。
image:用于图像处理(如灰度化等操作)。
Tesseract OCR:通过系统调用 Tesseract 进行验证码识别。
首先,确保在你的系统中安装了 Tesseract OCR,可以通过包管理器安装:

bash

sudo apt install tesseract-ocr
然后,在 pubspec.yaml 文件中添加以下依赖:

yaml

dependencies:
http: ^0.13.3
image: ^3.0.1
2. 下载验证码图片
我们使用 Dart 的 http 库发送 HTTP 请求下载验证码图片并保存到本地:

dart

import 'dart:io';
import 'package:http/http.dart' as http;

Future downloadCaptcha(String url, String savePath) async {
final response = await http.get(Uri.parse(url));
final file = File(savePath);
await file.writeAsBytes(response.bodyBytes);
print('验证码图片已保存为 $savePath');
}

void main() async {
final captchaUrl = 'https://captcha7.scrape.center/captcha.png';
final savePath = 'captcha.png';
await downloadCaptcha(captchaUrl, savePath);
}
3. 图像处理和 OCR 识别
接下来,我们使用 image 库将验证码图片转化为灰度图像,并调用系统中的 Tesseract OCR 进行识别:

dart

import 'dart:io';
import 'package:image/image.dart';
import 'dart:convert';

void preprocessImage(String inputPath, String outputPath) {
final image = decodeImage(File(inputPath).readAsBytesSync())!;
final grayscale = grayscale(image);
File(outputPath).writeAsBytesSync(encodePng(grayscale));
print('处理后的验证码图片已保存为 $outputPath');
}

Future recognizeCaptcha(String imagePath) async {
final result = await Process.run('tesseract', [imagePath, 'stdout']);
return result.stdout;
}

void main() async {
final processedPath = 'captcha_processed.png';

preprocessImage('captcha.png', processedPath);

final captchaText = await recognizeCaptcha(processedPath);
print('识别结果: $captchaText');
}
4. 自动化登录
最后,我们使用 Dart 的 http 库发送 POST 请求,模拟登录操作,并传递用户名、密码和识别出的验证码。

dart

import 'package:http/http.dart' as http;

Future login(String username, String password, String captcha) async {
final response = await http.post(
Uri.parse('https://captcha7.scrape.center/login'),
body: {
'username': username,
'password': password,
'captcha': captcha.trim(),
},
);

if (response.statusCode == 200) {
print('登录成功');
} else {
print('登录失败');
}
}

void main() async {
final processedPath = 'captcha_processed.png';

// 预处理并识别验证码
final captchaText = await recognizeCaptcha(processedPath);

// 自动化登录
await login('admin', 'admin', captchaText);
}

标签:http,String,登录,验证码,dart,captcha,Dart,final
From: https://www.cnblogs.com/ocr1/p/18472036

相关文章

  • mac管理flutter和dart sdk版本
    前言如果要用flutter开发ios和Andriod、windows和Mac系统的多兼容App,那么我们是需要用Mac系统开发的,但是要注意需要同时配置fvm管理flutter多版本以及本地安装dart和对应的dart sdk的版本并根据自己的项目要求来切换。 一、fvm--flutter多版本管理工具#使用brew......
  • 网站后台登录密码忘记了怎么办
    如果忘记了网站后台的登录密码,可以按照以下步骤尝试找回或重置密码:查看邮箱:如果你在注册或初次设置后台账号时绑定了邮箱,通常可以通过点击“忘记密码”链接,系统会向你的邮箱发送一封包含重置密码链接的邮件。联系管理员:如果你没有绑定邮箱或者无法访问绑定的邮箱,那么联系网......
  • SpringSecurity + Spnego + Kerberos 实现AD域单点登录
    SpringSecurity+Spnego+Kerberos实现AD域单点登录文章目录SpringSecurity+Spnego+Kerberos实现AD域单点登录前言一、域是什么?二、单点登录是什么?三、如何实现四、Kerberos五、Spnego介绍六、spring-ldap连接域实现域用户管理七、单点登录案例前言本文......
  • selenium登录B站,实现验证码识别登录
    Selenium+超级鹰登录B站需要使用到的包seleniumtimechaojiyingimporttimefromchaojiyingimportChaojiying_Clientfromselenium.webdriverimportChromefromselenium.webdriverimportActionChains注意,如果没有使用过超级鹰的经验可以先看一下开发文档,下......
  • C#图像处理与OCR:从验证码识别到文本提取 Tesseract实现验证码识别:本地化
    以下示例代码中,涉及到的知识点主要包括图像处理、验证码识别、Base64转换、图像预处理等。以下是详细的知识点梳理,以及相应的代码示例:1.图像加载与保存使用Image.FromFile加载本地图像,并使用Bitmap进行图像操作。Bitmap是图像处理的主要类,支持各种图像操作。代码......
  • 一:MYsql安装登录,服务开启和停止,和连接数据库
    一:MYsql安装登录,服务开启和停止,和连接数据库一、卸载不要的环境下面演示安装的版本为5.7版本,安装先把身份切换为root,方便操作首先先检测自己的环境,是否存在mariadb和系统自带MySQL,如果存在需要进行停止相应服务psajx|grepmariadbpsajx|grepmysql我的环境没有mariadb......
  • 识别图形验证码 (Elixir 示例)
    安装所需依赖在你的mix.exs文件中添加以下依赖:elixirdefpdepsdo[{:httpoison,"~>1.8"},{:mogrify,"~>0.7"},{:tesseract,"~>0.1"}]更多内容联系1436423940end然后运行mixdeps.get来安装这些库。下载并保存验证码图片使用HTTPoison下载验证码图片并保存......
  • 识别图形验证码 (Scala 示例)
    安装所需依赖在你的build.sbt文件中添加以下依赖:scalalibraryDependencies+="org.scalaj"%%"scalaj-http"%"2.4.2"下载并保存验证码图片使用scalaj-http下载验证码图片并保存到本地:scalaimportscalaj.http._importjava.nio.file.{Files,Paths}objectCaptch......
  • 识别图形验证码 (Julia 示例)
    安装所需依赖在JuliaREPL中使用以下命令安装所需的包:juliausingPkgPkg.add("HTTP")Pkg.add("Images")Pkg.add("Tesseract")下载并保存验证码图片使用HTTP下载验证码图片并保存到本地:juliausingHTTPusingFileIOfunctiondownload_captcha(url::String,save_pa......
  • 网站登录密码被修改了怎么办?
    当您发现网站登录密码被修改时,可以按照以下步骤尝试解决问题:检查是否自己忘记:回想近期是否有更改过密码的行为,有时可能是自己忘记了。使用找回密码功能:访问网站的登录页面,通常会有“忘记密码”或“找回密码”的链接。按照提示输入您的注册邮箱或手机号码。验证身份后......