在信息获取自动化越来越重要的今天,Python 爬虫技术成为数据采集的首选工具。在自动化登录、用户行为模拟、批量抓取等操作中,自动提交表单是一个核心环节。然而,验证码的存在成为拦路虎,意在防止机器人攻击。
本篇博客将通过 最新的 Python 工具链和深度学习模型,深入剖析如何自动提交带验证码的表单,做到从页面解析、验证码下载、图像识别到数据提交的全流程自动化。全文包含:
本文将以某些经典示例站点为例,展示通用技术。请遵守目标网站
robots.txt
与使用协议。
我们将使用以下技术栈:
类型 | 工具/库 | 功能 |
---|---|---|
网络请求 | requests + httpx |
表单交互与多线程 |
HTML解析 | BeautifulSoup4 + lxml |
提取表单结构 |
验证码识别 | Pillow , tesserocr , EasyOCR , CNN |
图像预处理与识别 |
模拟行为 | selenium |
JS动态页面处理 |
模型训练 | PyTorch |
字符识别深度学习模型 |
数据处理 | pandas , numpy |
结构化存储与分析 |
日志与异常处理 | logging |
调试与日志持久化 |
典型表单的 HTML 长这样:
html
复制编辑
action="/login"
是提交地址POST
我们重点攻克第一类。处理流程:
python
复制编辑
import requests
from PIL import Image
from io import BytesIO
captcha_url = 'https://example.com/captcha.jpg'
session = requests.Session()
response = session.get(captcha_url)
image = Image.open(BytesIO(response.content))
image.save('captcha.jpg')
python
复制编辑
from PIL import ImageFilter, ImageOps
def preprocess_image(img_path):
img = Image.open(img_path).convert('L') # 灰度化
img = ImageOps.invert(img) # 黑白翻转
img = img.filter(ImageFilter.MedianFilter()) # 中值滤波去噪
threshold = 128
img = img.point(lambda p: p > threshold and 255)
return img
python
复制编辑
import tesserocr
def ocr_recognize(img):
return tesserocr.image_to_text(img).strip()
img = preprocess_image('captcha.jpg')
captcha_text = ocr_recognize(img)
print(f"识别验证码:{captcha_text}")
如识别准确率较低,可以自训练CNN模型:
python
复制编辑
# 简化版CNN结构
import torch.nn as nn
class CaptchaCNN(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(1, 32, 3, 1),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.fc = nn.Sequential(
nn.Linear(32 * 13 * 30, 128),
nn.ReLU(),
nn.Linear(128, 36) # 数字 + 字母
)
def forward(self, x):
x = self.conv(x)
x = x.view(-1, 32 * 13 * 30)
return self.fc(x)
建议使用
EasyOCR
或预训练模型来避免重复造轮子。
python
复制编辑
form_data = {
'username': 'myuser',
'password': 'mypassword',
'captcha': captcha_text
}
headers = {
'User-Agent': 'Mozilla/5.0',
'Referer': 'https://example.com/login'
}
response = session.post('https://example.com/login', data=form_data, headers=headers)
print('登录结果:', response.status_code)
css
复制编辑
auto_captcha_login/
├── main.py
├── captcha.jpg
├── model/
│ └── cnn_model.pt
├── utils/
│ └── image_utils.py
│ └── ocr.py
├── logs/
│ └── app.log
python
复制编辑
# main.py
from utils.image_utils import preprocess_image
from utils.ocr import recognize_captcha
import requests
session = requests.Session()
img_url = 'https://example.com/captcha'
form_url = 'https://example.com/login'
# 1. 下载验证码
resp = session.get(img_url)
with open('captcha.jpg', 'wb') as f:
f.write(resp.content)
# 2. 图像识别
img = preprocess_image('captcha.jpg')
captcha_text = recognize_captcha(img)
# 3. 表单提交
payload = {
'username': 'admin',
'password': '123456',
'captcha': captcha_text
}
r = session.post(form_url, data=payload)
print("登录响应:", r.text)
网站策略 | 爬虫对策 |
---|---|
增加验证码复杂度 | 用深度学习提升识别 |
加入CSRF Token | 提前解析网页并携带 token |
滑块/拖动验证 | 使用 selenium 模拟行为 |
OCR干扰字符 | 图像增强 + 分割 + 模型 |
对于 JavaScript 动态生成验证码或需要模拟点击场景,Selenium 是必备工具:
python
复制编辑
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get('https://example.com/login')
# 下载验证码
captcha_img = driver.find_element(By.ID, 'captcha_img')
captcha_img.screenshot('captcha.png')
# 识别并填表
captcha = recognize_captcha(preprocess_image('captcha.png'))
driver.find_element(By.NAME, 'username').send_keys('admin')
driver.find_element(By.NAME, 'password').send_keys('123456')
driver.find_element(By.NAME, 'captcha').send_keys(captcha)
driver.find_element(By.ID, 'login_btn').click()
time.sleep(3)
driver.quit()
python
复制编辑
headers = {'User-Agent': fake_user_agent.random}
time.sleep(random.uniform(1, 3))
python
复制编辑
proxies = {
"http": "http://123.123.123.123:8080",
"https": "http://123.123.123.123:8080"
}
r = requests.get(url, proxies=proxies)
python
复制编辑
from fake_useragent import UserAgent
ua = UserAgent()
headers = {'User-Agent': ua.random}
自动提交表单,尤其是含验证码的交互,是高级爬虫工程中的必备能力。本文完整演示了:
推荐进一步学习: