阿拉伯语、波斯语和希伯来语网站提供从右到左 (RTL) 脚本的验证码,挑战标准 OCR 和文本注入。阿拉伯字符根据位置(词首、词中、词尾或孤立形式)进行不同的连接,这使得图像验证码识别变得更加困难。结合影响元素定位的 RTL 页面布局,这些站点需要特定的处理。
RTL 验证码挑战
| 挑战 | 细节 |
|---|---|
| 人物连接 | 阿拉伯字母根据相邻字符改变形状 |
| 从右到左阅读 | 验证码中的文本从右向左读取 |
| 混合方向 | A数字和拉丁文本与阿拉伯语混合(双向) |
| 变音符号 | /below 字符上方的点和标记(شاين 与 ساين) |
| RTL 页面布局 | 表单元素和 CAPTCHA 放置位置与 LTR 不同 |
Python:阿拉伯语图像验证码
import requests
import base64
import time
API_KEY = "YOUR_API_KEY"
SUBMIT_URL = "https://ocr.captchaai.com/in.php"
RESULT_URL = "https://ocr.captchaai.com/res.php"
def solve_arabic_captcha(image_path: str) -> str:
"""Solve an Arabic script image CAPTCHA."""
with open(image_path, "rb") as f:
image_b64 = base64.b64encode(f.read()).decode()
resp = requests.post(SUBMIT_URL, data={
"key": API_KEY,
"method": "base64",
"body": image_b64,
"language": 2, # Non-Latin character support
"json": 1,
}, timeout=30).json()
if resp.get("status") != 1:
raise RuntimeError(f"Submit: {resp.get('request')}")
task_id = resp["request"]
for _ in range(24):
time.sleep(5)
poll = requests.get(RESULT_URL, params={
"key": API_KEY, "action": "get", "id": task_id, "json": 1,
}, timeout=15).json()
if poll.get("request") == "CAPCHA_NOT_READY":
continue
if poll.get("status") == 1:
return poll["request"]
raise RuntimeError(f"Solve: {poll.get('request')}")
raise RuntimeError("Timeout")
def solve_arabic_captcha_from_url(session: requests.Session,
captcha_url: str) -> str:
"""Download and solve an Arabic CAPTCHA from a URL."""
resp = session.get(captcha_url, timeout=15)
image_b64 = base64.b64encode(resp.content).decode()
submit = requests.post(SUBMIT_URL, data={
"key": API_KEY,
"method": "base64",
"body": image_b64,
"language": 2,
"json": 1,
}, timeout=30).json()
if submit.get("status") != 1:
raise RuntimeError(f"Submit: {submit.get('request')}")
task_id = submit["request"]
for _ in range(24):
time.sleep(5)
poll = requests.get(RESULT_URL, params={
"key": API_KEY, "action": "get", "id": task_id, "json": 1,
}, timeout=15).json()
if poll.get("request") == "CAPCHA_NOT_READY":
continue
if poll.get("status") == 1:
return poll["request"]
raise RuntimeError(f"Solve: {poll.get('request')}")
raise RuntimeError("Timeout")
# --- RTL-aware form submission ---
def submit_form_with_arabic_captcha(
form_url: str,
captcha_url: str,
form_data: dict,
captcha_field: str = "captcha",
) -> requests.Response:
"""Complete an Arabic website form with CAPTCHA."""
session = requests.Session()
session.headers.update({
"Accept-Language": "ar-SA,ar;q=0.9",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
})
# Load the form page to establish session
session.get(form_url, timeout=15)
# Solve the CAPTCHA
captcha_text = solve_arabic_captcha_from_url(session, captcha_url)
print(f"Arabic CAPTCHA solved: {captcha_text}")
# Submit with the solved text
form_data[captcha_field] = captcha_text
response = session.post(form_url, data=form_data, timeout=30)
return response
# --- Usage ---
# Simple Arabic image CAPTCHA
text = solve_arabic_captcha("arabic_captcha.png")
print(f"Arabic text: {text}")
# Form submission on Arabic site
response = submit_form_with_arabic_captcha(
form_url="https://example.sa/registration",
captcha_url="https://example.sa/captcha/generate",
form_data={
"name": "اسم المستخدم",
"email": "user@example.com",
},
)
JavaScript:阿拉伯语和 RTL 验证码
const API_KEY = "YOUR_API_KEY";
const SUBMIT_URL = "https://ocr.captchaai.com/in.php";
const RESULT_URL = "https://ocr.captchaai.com/res.php";
const fs = require("fs");
async function solveArabicCaptcha(imagePath) {
const imageB64 = fs.readFileSync(imagePath, "base64");
const body = new URLSearchParams({
key: API_KEY,
method: "base64",
body: imageB64,
language: "2",
json: "1",
});
const resp = await (await fetch(SUBMIT_URL, { method: "POST", body })).json();
if (resp.status !== 1) throw new Error(`Submit: ${resp.request}`);
const taskId = resp.request;
for (let i = 0; i < 24; i++) {
await new Promise((r) => setTimeout(r, 5000));
const url = `${RESULT_URL}?key=${API_KEY}&action=get&id=${taskId}&json=1`;
const poll = await (await fetch(url)).json();
if (poll.request === "CAPCHA_NOT_READY") continue;
if (poll.status === 1) return poll.request;
throw new Error(`Solve: ${poll.request}`);
}
throw new Error("Timeout");
}
// Inject CAPTCHA token into RTL page with Playwright
async function solveAndInjectRTL(page) {
// RTL pages may position the CAPTCHA differently
const captchaImg = await page.locator("img[id*='captcha'], img[class*='captcha']");
const imgSrc = await captchaImg.getAttribute("src");
// Download the image
const buffer = await (await fetch(imgSrc)).arrayBuffer();
const imageB64 = Buffer.from(buffer).toString("base64");
// Solve
const body = new URLSearchParams({
key: API_KEY, method: "base64", body: imageB64,
language: "2", json: "1",
});
const resp = await (await fetch(SUBMIT_URL, { method: "POST", body })).json();
if (resp.status !== 1) throw new Error(`Submit: ${resp.request}`);
const taskId = resp.request;
for (let i = 0; i < 24; i++) {
await new Promise((r) => setTimeout(r, 5000));
const url = `${RESULT_URL}?key=${API_KEY}&action=get&id=${taskId}&json=1`;
const poll = await (await fetch(url)).json();
if (poll.request === "CAPCHA_NOT_READY") continue;
if (poll.status === 1) {
// Fill the input — RTL input handles text direction automatically
await page.locator("input[name*='captcha']").fill(poll.request);
return poll.request;
}
throw new Error(`Solve: ${poll.request}`);
}
}
// Usage
const text = await solveArabicCaptcha("arabic_captcha.png");
console.log(`Arabic text: ${text}`);
支持的 RTL 脚本
| 脚本 | 语言 | 示例字符 |
|---|---|---|
| 阿拉伯 | 阿拉伯语、乌尔都语、普什图语 | Ø1رءي - Ø£Ø¡ØØØ̄ÙØ© |
| 波斯语/Persian | 波斯语 | ÙØ§Ø±Ø3ÛŒ - ØØ±ÙˆÙ |
| 希伯来语 | 希伯来语 | ×mבסית - ×ותיות |
故障排除
| 问题 | 原因 | 处理方式 |
|---|---|---|
| 输出中的阿拉伯文本反转 | 客户端将 RTL 文本显示为 LTR | 将输出包装在 \u202B(RTL 嵌入)中或使用 RTL 感知显示 |
| 缺少变音符号 | 低分辨率图像 | 使用更高分辨率的验证码图像 |
| 双向文本(阿拉伯文+数字)混乱 | BiDi算法应用不一致 | 使用 Unicode 标记显式处理方向字符 |
| 阿拉伯语输入导致表单提交失败 | 编码不匹配 | 在 Content-Type 标头中使用 charset=UTF-8 |
| 验证码位置与预期不同 | RTL 布局镜像元素位置 | 使用 CSS 选择器而不是基于位置的检测 |
常问问题
CaptchaAI 是否处理连接的阿拉伯语脚本?
是的。阿拉伯字符在相邻时连接 - 相同的字母根据其在单词中的位置而看起来不同。CaptchaAI 的 Image/OCR 解算器可识别连接的阿拉伯文字,包括首字母、中间字母、结尾字母和独立字母形式。
如何处理波斯语验证码与阿拉伯语验证码?
两者都使用阿拉伯文字,但带有附加字符(如波斯语中的 Ùο、Ú†、Ú~、Ú́)。两者均使用 language=2。 CaptchaAI 自动识别扩展字符集。
RTL 页面布局会影响验证码检测吗?
RTL 布局镜像页面 - 与 LTR 页面相比,表单和验证码可能出现在相反的两侧。使用 CSS 选择器或 ID 来定位验证码元素,而不是依赖视觉位置。
下一步
解决阿拉伯语和 RTL 网站上的验证码问题 -获取您的 CaptchaAI API 密钥并处理任何 RTL 字符集。
相关指南:
- 解决中文网站上的验证码
- 多语言图像验证码字符集
- 验证码本地化和语言设置