
Ethan Collins
Pattern Recognition Specialist

网页自动化需要既强大又易于使用的工具。然而,现代网站部署了复杂的反机器人措施和验证码,这可能会阻止自动化脚本的运行。
Helium 和 CapSolver 的结合提供了一个优雅的解决方案:
这些工具共同实现了自动处理验证码的无缝网页自动化。
本指南将帮助您实现三个核心目标:
Helium 是一个 Python 库,使 Selenium 的使用变得简单得多。它提供了一个高级 API,允许您用自然语言编写浏览器自动化。
click("Submit") 而不是复杂的 XPath 选择器write("Hello", into="Search")# 安装 Helium
pip install helium
# 安装 requests 库用于 CapSolver API
pip install requests
from helium import *
# 启动浏览器并导航
start_chrome("https://wikipedia.org")
# 在搜索框中输入
write("Python programming", into=S("input[name='search']"))
# 点击搜索按钮
click(Button("Search"))
# 检查文本是否存在
if Text("Python").exists():
print("找到 Python 文章!")
# 关闭浏览器
kill_browser()
CapSolver 是一个基于人工智能的自动验证码解决服务,支持多种验证码类型。它提供了一个简单的 API,允许您提交验证码挑战并在几秒钟内收到解决方案。
优惠:注册时使用代码
HELIUM可获得额外积分!
https://api.capsolver.comhttps://api-stable.capsolver.com在将 Helium 与 CapSolver 结合之前,网页自动化面临以下挑战:
| 挑战 | 影响 |
|---|---|
| 验证码挑战 | 需要手动解决,中断自动化 |
| 复杂的选择器 | Selenium 需要冗长的 XPath/CSS 选择器 |
| 时间问题 | 元素未就绪时访问 |
| 代码可读性 | 自动化脚本难以维护 |
Helium + CapSolver 集成通过简洁易读的代码解决了这些挑战。
API 集成方法为您提供对验证码解决过程的完全控制,并且适用于任何验证码类型。
pip install helium requests
import time
import requests
from helium import *
CAPSOLVER_API_KEY = "YOUR_API_KEY"
CAPSOLVER_API = "https://api.capsolver.com"
def create_task(task_payload: dict) -> str:
"""创建验证码解决任务并返回任务 ID。"""
response = requests.post(
f"{CAPSOLVER_API}/createTask",
json={
"clientKey": CAPSOLVER_API_KEY,
"task": task_payload
}
)
result = response.json()
if result.get("errorId") != 0:
raise Exception(f"CapSolver 错误: {result.get('errorDescription')}")
return result["taskId"]
def get_task_result(task_id: str, max_attempts: int = 120) -> dict:
"""轮询任务结果直到解决或超时。"""
for _ in range(max_attempts):
response = requests.post(
f"{CAPSOLVER_API}/getTaskResult",
json={
"clientKey": CAPSOLVER_API_KEY,
"taskId": task_id
}
)
result = response.json()
if result.get("status") == "ready":
return result["solution"]
elif result.get("status") == "failed":
raise Exception(f"任务失败: {result.get('errorDescription')}")
time.sleep(1)
raise TimeoutError("验证码解决超时")
def solve_captcha(task_payload: dict) -> dict:
"""完成验证码解决工作流。"""
task_id = create_task(task_payload)
return get_task_result(task_id)
您也可以使用 CapSolver 浏览器扩展与 Helium 配合使用,实现自动验证码检测和解决。
config.js 文件中配置您的 API 密钥:// 在扩展文件夹中,编辑: assets/config.js
var defined = {
apiKey: "YOUR_CAPSOLVER_API_KEY", // 替换为您的实际 API 密钥
enabledForBlacklistControl: false,
blackUrlList: [],
enabledForRecaptcha: true,
enabledForRecaptchaV3: true,
enabledForTurnstile: true,
// ...其他设置
}
from helium import *
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_argument('--load-extension=/path/to/capsolver-extension')
start_chrome(options=options)
# 扩展会自动检测并解决验证码
注意: 扩展必须在配置有效 API 密钥后才能自动解决验证码。
此示例在 Google 的演示页面上解决 reCAPTCHA v2,并自动检测站点密钥:
import time
import requests
from helium import *
from selenium.webdriver import ChromeOptions
CAPSOLVER_API_KEY = "YOUR_API_KEY"
CAPSOLVER_API = "https://api.capsolver.com"
def solve_recaptcha_v2(site_key: str, page_url: str) -> str:
"""解决 reCAPTCHA v2 并返回令牌。"""
# 创建任务
response = requests.post(
f"{CAPSOLVER_API}/createTask",
json={
"clientKey": CAPSOLVER_API_KEY,
"task": {
"type": "ReCaptchaV2TaskProxyLess",
"websiteURL": page_url,
"websiteKey": site_key,
}
}
)
result = response.json()
if result.get("errorId") != 0:
raise Exception(f"错误: {result.get('errorDescription')}")
task_id = result["taskId"]
print(f"任务创建: {task_id}")
# 轮询结果
while True:
result = requests.post(
f"{CAPSOLVER_API}/getTaskResult",
json={
"clientKey": CAPSOLVER_API_KEY,
"taskId": task_id
}
).json()
if result.get("status") == "ready":
return result["solution"]["gRecaptchaResponse"]
elif result.get("status") == "failed":
raise Exception(f"失败: {result.get('errorDescription')}")
print(" 等待解决方案...")
time.sleep(1)
def main():
target_url = "https://www.google.com/recaptcha/api2/demo"
# 配置浏览器反检测
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
print("启动浏览器...")
start_chrome(target_url, options=options)
driver = get_driver()
try:
time.sleep(2)
# 从页面自动检测站点密钥
recaptcha_element = driver.find_element("css selector", ".g-recaptcha")
site_key = recaptcha_element.get_attribute("data-sitekey")
print(f"检测到站点密钥: {site_key}")
# 解决验证码
print("\n使用 CapSolver 解决 reCAPTCHA v2...")
token = solve_recaptcha_v2(site_key, target_url)
print(f"获取到令牌: {token[:50]}...")
# 注入令牌
print("\n注入令牌...")
driver.execute_script(f'''
var responseField = document.getElementById('g-recaptcha-response');
responseField.style.display = 'block';
responseField.value = '{token}';
''')
print("令牌注入成功!")
# 使用 Helium 的简单语法提交表单
print("\n提交表单...")
click("Submit")
time.sleep(3)
# 检查是否成功
if "Verification Success" in driver.page_source:
print("\n=== 成功!===")
print("reCAPTCHA 已解决并提交了表单!")
finally:
kill_browser()
if __name__ == "__main__":
main()
亲自测试一下:
python demo_recaptcha_v2.py
Cloudflare Turnstile 是最常见的验证码挑战之一。以下是解决方法:
import time
import requests
from helium import *
from selenium.webdriver import ChromeOptions
CAPSOLVER_API_KEY = "YOUR_API_KEY"
CAPSOLVER_API = "https://api.capsolver.com"
def solve_turnstile(site_key: str, page_url: str) -> str:
"""解决 Cloudflare Turnstile 并返回令牌。"""
response = requests.post(
f"{CAPSOLVER_API}/createTask",
json={
"clientKey": CAPSOLVER_API_KEY,
"task": {
"type": "AntiTurnstileTaskProxyLess",
"websiteURL": page_url,
"websiteKey": site_key,
}
}
)
result = response.json()
if result.get("errorId") != 0:
raise Exception(f"错误: {result.get('errorDescription')}")
task_id = result["taskId"]
while True:
result = requests.post(
f"{CAPSOLVER_API}/getTaskResult",
json={
"clientKey": CAPSOLVER_API_KEY,
"taskId": task_id
}
).json()
if result.get("status") == "ready":
return result["solution"]["token"]
elif result.get("status") == "failed":
raise Exception(f"失败: {result.get('errorDescription')}")
time.sleep(1)
def main():
target_url = "https://your-target-site.com"
turnstile_site_key = "0x4XXXXXXXXXXXXXXXXX" # 在页面源代码中查找
# 配置浏览器
options = ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')
start_chrome(target_url, options=options)
driver = get_driver()
try:
# 等待 Turnstile 加载
time.sleep(3)
# 解决验证码
print("解决 Turnstile...")
token = solve_turnstile(turnstile_site_key, target_url)
print(f"获取到令牌: {token[:50]}...")
# 注入令牌
driver.execute_script(f'''
document.querySelector('input[name="cf-turnstile-response"]').value = "{token}";
// 如果存在回调函数则触发
const callback = document.querySelector('[data-callback]');
if (callback) {{
const callbackName = callback.getAttribute('data-callback');
if (window[callbackName]) {{
window[callbackName]('{token}');
}}
}}
''')
# 使用 Helium 提交表单
if Button("Submit").exists():
click("Submit")
print("Turnstile 已绕过!")
finally:
kill_browser()
if __name__ == "__main__":
main()
reCAPTCHA v3 是基于评分的,不需要用户交互:
import time
import requests
from helium import *
from selenium.webdriver import ChromeOptions
CAPSOLVER_API_KEY = "YOUR_API_KEY"
CAPSOLVER_API = "https://api.capsolver.com"
def solve_recaptcha_v3(
site_key: str,
page_url: str,
action: str = "verify",
min_score: float = 0.7
) -> str:
"""使用指定操作和最低分数解决 reCAPTCHA v3。"""
response = requests.post(
f"{CAPSOLVER_API}/createTask",
json={
"clientKey": CAPSOLVER_API_KEY,
"task": {
"type": "ReCaptchaV3TaskProxyLess",
"websiteURL": page_url,
"websiteKey": site_key,
"pageAction": action,
"minScore": min_score
}
}
)
result = response.json()
if result.get("errorId") != 0:
raise Exception(f"错误: {result.get('errorDescription')}")
task_id = result["taskId"]
while True:
result = requests.post(
f"{CAPSOLVER_API}/getTaskResult",
json={
"clientKey": CAPSOLVER_API_KEY,
"taskId": task_id
}
).json()
if result.get("status") == "ready":
return result["solution"]["gRecaptchaResponse"]
elif result.get("status") == "failed":
raise Exception(f"失败: {result.get('errorDescription')}")
time.sleep(1)
def main():
target_url = "https://your-target-site.com"
recaptcha_v3_key = "6LcXXXXXXXXXXXXXXXXXXXXXXXXX"
# 设置无头浏览器用于 v3
options = ChromeOptions()
options.add_argument('--headless')
start_chrome(target_url, options=options)
driver = get_driver()
try:
# 使用 "login" 操作解决 reCAPTCHA v3
print("解决 reCAPTCHA v3...")
token = solve_recaptcha_v3(
recaptcha_v3_key,
target_url,
action="login",
min_score=0.9
)
# 注入令牌
driver.execute_script(f'''
var responseField = document.querySelector('[name="g-recaptcha-response"]');
if (responseField) {{
responseField.value = '{token}';
}}
// 如果存在回调函数则调用
if (typeof onRecaptchaSuccess === 'function') {{
onRecaptchaSuccess('{token}');
}}
''')
print("已绕过reCAPTCHA v3!")
finally:
kill_browser()
if __name__ == "__main__":
main()
配置Chrome使其更像普通浏览器:
from helium import *
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--window-size=1920,1080')
start_chrome(options=options)
使用Helium的简洁语法进行大部分操作,需要时访问Selenium:
from helium import *
start_chrome("https://target-site.com")
# 使用Helium进行简单交互
write("username", into="Email")
write("password", into="Password")
# 访问Selenium驱动进行复杂操作
driver = get_driver()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
# 返回Helium
click("Login")
通过添加随机延迟避免触发速率限制:
import random
import time
def human_delay(min_sec=1.0, max_sec=3.0):
"""随机延迟以模拟人类行为。"""
time.sleep(random.uniform(min_sec, max_sec))
# 在操作之间使用
click("Next")
human_delay()
write("data", into="Input")
始终为CAPTCHA解决实现适当的错误处理:
def solve_with_retry(task_payload: dict, max_retries: int = 3) -> dict:
"""带重试逻辑的CAPTCHA解决方法。"""
for attempt in range(max_retries):
try:
return solve_captcha(task_payload)
except TimeoutError:
if attempt < max_retries - 1:
print(f"超时,重试... ({attempt + 1}/{max_retries})")
time.sleep(5)
else:
raise
except Exception as e:
if "balance" in str(e).lower():
raise # 不重试余额错误
if attempt < max_retries - 1:
time.sleep(2)
else:
raise
使用无头模式进行后台自动化:
from helium import *
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
start_chrome("https://target-site.com", options=options)
| 操作 | Selenium | Helium |
|---|---|---|
| 点击按钮 | driver.find_element(By.XPATH, "//button[text()='Submit']").click() |
click("Submit") |
| 输入文本 | driver.find_element(By.NAME, "email").send_keys("test@test.com") |
write("test@test.com", into="Email") |
| 按下回车 | element.send_keys(Keys.ENTER) |
press(ENTER) |
| 检查文本是否存在 | "Welcome" in driver.page_source |
Text("Welcome").exists() |
Helium与CapSolver的集成创建了一个优雅的网页自动化工具包:
无论您是构建网络爬虫、自动化测试系统还是数据收集管道,这种组合都能提供简洁与强大的功能。
附加提示: 在CapSolver注册时使用代码
HELIUM可获得额外积分!
Helium使Selenium更易于使用:
CapSolver支持所有主要的CAPTCHA类型。Cloudflare Turnstile和reCAPTCHA v2/v3的成功率最高。这种集成可以与CapSolver支持的任何CAPTCHA无缝配合。
可以!Helium通过ChromeOptions支持无头模式。对于reCAPTCHA v3和基于令牌的CAPTCHA,无头模式可以完美运行。对于v2可见式CAPTCHA,使用有头模式可能效果更好。
在页面源代码中查找:
data-sitekey属性或cf-turnstile元素g-recaptcha div上的data-sitekey属性常见解决方案:
可以!调用get_driver()以访问底层的Selenium WebDriver执行任何Helium未直接覆盖的操作。