本文为「视觉爬虫开发:通过 Puppeteer 截图 + CV 定位动态元素坐标」的速查指南,帮助你快速掌握在小红书(https://www.xiaohongshu.com/)上使用 Puppeteer 结合 OpenCV 实现视频截图与评论采集的核心思路与代码示例。文章分为四大部分:功能点列表、常用代码片段、配置建议、快速测试方式,并集成爬虫代理设置,以便直接在项目中复用。
const puppeteer = require('puppeteer');
(async () => {
// 启动无头浏览器,接入亿牛云爬虫代理 www.16yun.cn
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--proxy-server=tcp://t.16yun.cn:31111' // 代理域名:端口 :contentReference[oaicite:3]{index=3}
]
});
const page = await browser.newPage();
// 设置代理认证(Tunnel 模式下 Puppeteer 自动支持用户名/密码)
await page.authenticate({
username: 'YOUR_PROXY_USER', // 亿牛云用户名 :contentReference[oaicite:4]{index=4}
password: 'YOUR_PROXY_PASS' // 亿牛云密码 :contentReference[oaicite:5]{index=5}
});
// 设置 Cookie 与 User-Agent
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ...'); // 模拟浏览器 UA
await page.setCookie({
name: 'sessionid',
value: 'YOUR_SESSION_ID',
domain: '.xiaohongshu.com'
});
// 跳转到小红书视频页
await page.goto('https://www.xiaohongshu.com/', { waitUntil: 'networkidle2' });
// 等待视频元素出现并截屏
const videoHandle = await page.waitForSelector('video'); // 定位视频元素
const boundingBox = await videoHandle.boundingBox();
await page.screenshot({
path: 'video_frame.png',
clip: {
x: boundingBox.x,
y: boundingBox.y,
width: boundingBox.width,
height: boundingBox.height
}
}); // 截取视频区域 :contentReference[oaicite:6]{index=6}
await browser.close();
})();
import cv2
import numpy as np
# 加载 Puppeteer 截图
screenshot = cv2.imread("video_frame.png", 0) # 灰度化
template = cv2.imread("button_template.png", 0) # 预先截图的按钮模板
# 执行多尺度模板匹配 :contentReference[oaicite:7]{index=7}
res = cv2.matchTemplate(screenshot, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
h, w = template.shape
bottom_right = (top_left[0] + w, top_left[1] + h)
# 可视化结果
cv2.rectangle(screenshot, top_left, bottom_right, 255, 2)
cv2.imwrite("matched.png", screenshot)
print(f"元素坐标:{top_left}")
// 在原 Puppeteer 脚本中继续:
const comments = await page.evaluate(() => {
// 滚动并加载更多评论
window.scrollBy(0, window.innerHeight);
// 假设评论项选择器为 .comment-item
return Array.from(document.querySelectorAll('.comment-item')).map(el => ({
user: el.querySelector('.user-name')?.innerText.trim(),
text: el.querySelector('.comment-text')?.innerText.trim()
}));
});
console.log(comments);
Proxy-Tunnel: 随机数
HTTP 头实现精确切换 (CSDN)。waitUntil: 'networkidle2'
与 page.waitForSelector()
确保视频及评论加载完成。npm install puppeteer
pip install opencv-python-headless
curl -x http://YOUR_PROXY_USER:YOUR_PROXY_PASS@t.16yun.cn:31111 https://httpbin.org/ip
node capture.js
# 检查生成的 video_frame.png 是否正确截取视频区域
python template_match.py
# 检查 matched.png 是否在目标位置画出矩形框
comments
,确认输出的用户名与评论内容是否符合预期。以上即为「视觉爬虫开发:通过 Puppeteer 截图 + CV 定位动态元素坐标」的速查指南,涵盖代理接入、Cookie/UA 设置、视频截图、元素定位与评论采集四大核心功能,助你快速上手并在小红书等动态站点实现可靠的视觉爬虫方案。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有