做过数据采集的兄弟都懂,写爬虫最痛苦的不是反爬对抗,而是无穷无尽的规则维护。
目标站点改个class名、换个div嵌套,昨晚还跑得好好的脚本今早就全线报错。传统爬虫本质是“硬编码匹配”,而2026年真正能解放生产力的,是用AI Agent把“规则解析”变成“语义理解”。
最近我在内部数据平台落地了一套Prompt驱动的零规则采集方案,彻底告别了XPath和CSS选择器。今天这篇不讲概念,只聊工程落地中踩过的坑和验证有效的实操路径。
一、 前期准备:重新定义“采集”这件事
在动手之前,必须先扭转一个认知:AI Agent不是用来替代HTTP请求的,而是用来替代“人眼定位+手写规则”这个环节的。
1. 什么是零规则采集?
简单说,就是你不再告诉程序“找class为product-price的span”,而是告诉Agent“提取当前页面所有商品的售价”。Agent通过多模态模型理解页面结构,自主完成元素定位与数据抽取。
2. 技术选型的核心考量
纯大模型API调用成本高、延迟大,不适合高频采集。我的方案是:轻量级视觉模型(YOLO)做区域裁剪 + 小参数VLM做语义提取 ,兼顾成本与准确率。
3. 环境依赖清单
- Python 3.10+
- ultralytics(YOLOv8/v11推理)
- transformers或ollama(本地VLM部署)
- playwright(仅用于渲染动态页面,不做元素定位)
二、 分步实操:从截图到结构化数据的完整链路
整套方案分为三步:页面快照、视觉定位、语义抽取。下面逐层拆解核心实现。
1. 页面快照与预处理
动态页面先用Playwright渲染,但注意:不要用它找元素 ,只负责拿到干净的截图。
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page(viewport={"width": 1920, "height": 1080})
page.goto("https://target.com/list")
page.wait_for_load_state("networkidle")
page.screenshot(path="page_snapshot.png")
browser.close()
这里的关键是设置固定viewport尺寸。后续YOLO模型训练和推理都依赖统一分辨率,尺寸不一致会导致定位偏移。
2. YOLO做区域裁剪:把整页变成“兴趣块”
直接把整张截图丢给VLM,token消耗爆炸且容易遗漏细节。正确做法是用YOLO先检测出商品卡片、表格行等语义区块,再逐个送检。
from ultralytics import YOLO
from PIL import Image
model = YOLO("card_detector.pt") # 自训练的区块检测模型
img = Image.open("page_snapshot.png")
results = model(img, conf=0.7)
crops = []
for box in results[0].boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
crops.append(img.crop((x1, y1, x2, y2)))
这一步将单页处理成本降低了80%以上。YOLO模型只需标注几百张样本,训练2小时即可达到可用精度,远比手写正则划算。
3. Prompt驱动语义抽取:零规则的核心
拿到裁剪后的区块图,用结构化Prompt让VLM输出JSON。Prompt要约束字段名和类型,别让模型自由发挥。
你是一个数据提取助手。请从图片中提取商品信息,严格按以下JSON格式返回:
{"name": "字符串", "price": "数字", "stock_status": "有货/缺货/未知"}
不要输出任何解释文字,仅返回合法JSON。
实测Qwen2.5-VL-7B在本地Ollama上运行,单张卡片提取耗时约1.2秒,准确率稳定在95%以上。对于价格、库存这类关键字段,可在Prompt中加入few-shot示例进一步提升鲁棒性。
三、 问题排查:上线后最容易踩的三个坑
这套方案看起来优雅,但工程落地时处处是暗礁。以下是我实际遇到的问题和解法:
1. VLM幻觉导致字段错位
模型偶尔会把促销价识别为原价,或凭空编造不存在的字段。
解法: 增加后置校验层。用正则验证价格格式,用枚举值校验状态字段。异常结果标记为低置信度,进入人工复核队列而非直接入库。
2. 长列表页面漏采
YOLO检测依赖单屏截图,懒加载内容无法覆盖。
解法: 编写自动滚动脚本,每滚动一屏截取一次,对截图做哈希去重后再送入检测流程。避免重复处理相同内容。
3. 本地VLM显存不足
7B模型在消费级显卡上容易OOM,尤其批量处理时。
解法: 启用vllm或llama.cpp的量化推理,INT4量化后显存占用减半,速度损失可控。或者采用异步队列,控制并发数不超过显存安全阈值。
四、 架构总览:Agent采集流水线
为了更直观理解整体协作关系,下面是我实际使用的处理流程图:
#mermaid-svg-4bexmO1Oie3BzVgY{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-4bexmO1Oie3BzVgY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-4bexmO1Oie3BzVgY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-4bexmO1Oie3BzVgY .error-icon{fill:#552222;}#mermaid-svg-4bexmO1Oie3BzVgY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-4bexmO1Oie3BzVgY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-4bexmO1Oie3BzVgY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-4bexmO1Oie3BzVgY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-4bexmO1Oie3BzVgY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-4bexmO1Oie3BzVgY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-4bexmO1Oie3BzVgY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-4bexmO1Oie3BzVgY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-4bexmO1Oie3BzVgY .marker.cross{stroke:#333333;}#mermaid-svg-4bexmO1Oie3BzVgY svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-4bexmO1Oie3BzVgY p{margin:0;}#mermaid-svg-4bexmO1Oie3BzVgY .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-4bexmO1Oie3BzVgY .cluster-label text{fill:#333;}#mermaid-svg-4bexmO1Oie3BzVgY .cluster-label span{color:#333;}#mermaid-svg-4bexmO1Oie3BzVgY .cluster-label span p{background-color:transparent;}#mermaid-svg-4bexmO1Oie3BzVgY .label text,#mermaid-svg-4bexmO1Oie3BzVgY span{fill:#333;color:#333;}#mermaid-svg-4bexmO1Oie3BzVgY .node rect,#mermaid-svg-4bexmO1Oie3BzVgY .node circle,#mermaid-svg-4bexmO1Oie3BzVgY .node ellipse,#mermaid-svg-4bexmO1Oie3BzVgY .node polygon,#mermaid-svg-4bexmO1Oie3BzVgY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-4bexmO1Oie3BzVgY .rough-node .label text,#mermaid-svg-4bexmO1Oie3BzVgY .node .label text,#mermaid-svg-4bexmO1Oie3BzVgY .image-shape .label,#mermaid-svg-4bexmO1Oie3BzVgY .icon-shape .label{text-anchor:middle;}#mermaid-svg-4bexmO1Oie3BzVgY .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-4bexmO1Oie3BzVgY .rough-node .label,#mermaid-svg-4bexmO1Oie3BzVgY .node .label,#mermaid-svg-4bexmO1Oie3BzVgY .image-shape .label,#mermaid-svg-4bexmO1Oie3BzVgY .icon-shape .label{text-align:center;}#mermaid-svg-4bexmO1Oie3BzVgY .node.clickable{cursor:pointer;}#mermaid-svg-4bexmO1Oie3BzVgY .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-4bexmO1Oie3BzVgY .arrowheadPath{fill:#333333;}#mermaid-svg-4bexmO1Oie3BzVgY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-4bexmO1Oie3BzVgY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-4bexmO1Oie3BzVgY .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-4bexmO1Oie3BzVgY .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-4bexmO1Oie3BzVgY .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-4bexmO1Oie3BzVgY .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-4bexmO1Oie3BzVgY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-4bexmO1Oie3BzVgY .cluster text{fill:#333;}#mermaid-svg-4bexmO1Oie3BzVgY .cluster span{color:#333;}#mermaid-svg-4bexmO1Oie3BzVgY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-4bexmO1Oie3BzVgY .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-4bexmO1Oie3BzVgY rect.text{fill:none;stroke-width:0;}#mermaid-svg-4bexmO1Oie3BzVgY .icon-shape,#mermaid-svg-4bexmO1Oie3BzVgY .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-4bexmO1Oie3BzVgY .icon-shape p,#mermaid-svg-4bexmO1Oie3BzVgY .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-4bexmO1Oie3BzVgY .icon-shape .label rect,#mermaid-svg-4bexmO1Oie3BzVgY .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-4bexmO1Oie3BzVgY .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-4bexmO1Oie3BzVgY .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-4bexmO1Oie3BzVgY :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
目标URL
Playwright渲染截图
YOLO区块检测
检测到N个区块
区块1裁剪
区块2裁剪
区块N裁剪
VLM + Prompt语义提取
JSON校验与清洗
结构化数据入库
整个链路中,Playwright只做渲染,YOLO只做定位,VLM只做语义理解。各司其职,没有一环承担超出其能力范围的任务 ,这是系统稳定的关键。
五、 实战总结与边界提醒
这套Prompt驱动的零规则采集方案,在我负责的3个业务场景中已稳定运行两个月。最大的收益不是开发效率提升,而是维护成本趋近于零 ——目标站点改版5次,采集逻辑未做任何修改。
但有几点必须清醒认识:
- 它不适合高频实时采集。 单页处理秒级延迟,适合离线T+1或低频监控场景。追求毫秒级响应还是得回归传统接口逆向。
- 模型需要持续迭代。 新类型的页面布局出现时,需补充YOLO标注样本。这不是“一劳永逸”的魔法,而是用模型维护替代规则维护。
- 合规红线不可触碰。 AI能力再强,也不能用于采集个人隐私、突破授权边界。技术中立,使用有责。
最后说一句大实话:零规则采集不是要消灭传统爬虫,而是在规则维护成本过高的场景下提供一个务实的备选方案。选对工具比追逐新概念更重要。
工程师笔记 :从写XPath到写Prompt,变的是交互方式,不变的是对数据质量和系统稳定性的执着。如果你也在被规则维护折磨,不妨试试这条路径。遇到具体问题欢迎评论区交流,看到都会回复。


