ImageToTextTask
这是常规验证码,即包含文本的图片,需要输入相应的文本。
请求参数
type
<string>requiredImageToTextTask
body
<string>required以base64编码的文件主体。确保发送时没有换行符。
capMonsterModule
<string>optional识别模块的名称,例如“yandex”。可以通过这里找到模块名称及所有可用模块的列表。
示例: yandex, special 及其他
recognizingThreshold
<integer>optional验证码识别阈值,可取值范围为0到100。例如,如果将 recognizingThreshold 设置为90,并且任务以80的置信度解决,则不会收费。在这种情况下,用户会收到 ERROR_CAPTCHA_UNSOLVABLE 的响应。 种设置扣款阈值的替代方法在 该文章 中有描述。
case
<boolean>optionaltrue - 如果验证码区分大小写。
numeric
<integer>optional1 - 如果验证码仅包含数字。
可能的值: 0, 1
math
<boolean>optionalfalse — 未定义;
true — 如果验证码需要数学操作(例如:验证码 2 + 6 = 将返回值 8)。
Base64 是一种 数据编码方法,允许将二进制数据表示为文本。以下是使用开发者工具中的控制台获取验证码图像的 base64 格式的示例:
const captchaUrl = 'https://example.com/captcha.jpg';
function loadAndEncodeCaptchaToBase64(url) {
fetch(url)
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
const base64Data = reader.result;
console.log('Base64 编码的验证码:, base64Data);
};
})
.catch(error => {
console.error('加载或编码验证码时出错:', error);
});
}
loadAndEncodeCaptchaToBase64(captchaUrl);
创建任务方法
POST
https://api.capmonster.cloud/createTask
要求
{
"clientKey":"API_KEY",
"task": {
"type":"ImageToTextTask",
"body":"BASE64_BODY_HERE!"
}
}
回应
{
"errorId":0,
"taskId":407533072
}
获取任务结果方法
使用getTaskResult方法获取验证码的解决方案。根据系统负载,您将在300毫秒到6秒的时间范围内收到答案。
POST
https://api.capmonster.cloud/getTaskResult
要求
{
"clientKey":"API_KEY",
"taskId": 407533072
}
回应
{
"errorId":0,
"status":"ready",
"solution": {
"text":"answer"
}
}
属性 | 类型 | 描述 |
---|---|---|
text | String | 验证码答案 |
使用 SDK 库
- JavaScript
- Python
- C#
// https://github.com/ZennoLab/capmonstercloud-client-js
import { CapMonsterCloudClientFactory, ClientOptions, ImageToTextRequest } from '@zennolab_com/capmonstercloud-client';
document.addEventListener('DOMContentLoaded', async () => {
const cmcClient = CapMonsterCloudClientFactory.Create(new ClientOptions({ clientKey: '<your capmonster.cloud API key>' }));
console.log(await cmcClient.getBalance());
const imageToTextRequest = new ImageToTextRequest({
body: 'some base64 body',
CapMonsterModule: CapMonsterModules.YandexWave,
Case: true,
numeric: 1,
recognizingThreshold: 65,
math: false,
});
console.log(await cmcClient.Solve(imageToTextRequest));
});
# https://github.com/ZennoLab/capmonstercloud-client-python
import asyncio
import base64
from capmonstercloudclient import CapMonsterClient, ClientOptions
from capmonstercloudclient.requests import ImageToTextRequest
client_options = ClientOptions(api_key="your_api_key") # Replace with your CapMonsterCloud API key
cap_monster_client = CapMonsterClient(options=client_options)
image_base64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgc…wwzqR4U/yZ//Z"
image_bytes = base64.b64decode(image_base64)
image_to_text_request = ImageToTextRequest(
image_bytes=image_bytes,
module_name=None, # Optional, can specify a module name if needed
threshold=50, # Optional, set a threshold value between 1 and 100
case=True, # Optional, specify whether case sensitivity is required
numeric=0, # Optional, set numeric flag (0 or 1)
math=False # Optional, specify whether math operations are involved
)
async def solve_captcha():
return await cap_monster_client.solve_captcha(image_to_text_request)
responses = asyncio.run(solve_captcha())
print(responses)
// https://github.com/ZennoLab/capmonstercloud-client-dotnet
using Zennolab.CapMonsterCloud.Requests;
using Zennolab.CapMonsterCloud;
class Program
{
static async Task Main(string[] args)
{
var clientOptions = new ClientOptions
{
ClientKey = "your_api_key" // Replace with your CapMonster Cloud API key
};
var cmCloudClient = CapMonsterCloudClientFactory.Create(clientOptions);
var imageToTextRequest = new ImageToTextRequest
{
Body = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMj", // Base64 encoded image
CapMonsterModule = "None", // Optional, can specify a module name if needed
RecognizingThreshold = 70, // Optional, set a threshold value between 1 and 100
CaseSensitive = true, // Optional, specify whether case sensitivity is required
Numeric = false,
Math = false
};
var imageToTextResult = await cmCloudClient.SolveAsync(imageToTextRequest);
Console.WriteLine("Captcha Solved: " + imageToTextResult.Solution.Value);
}
}