香港服务器搭建Headless Chrome/Puppeteer爬虫运行环境完整教程
为什么爬虫要跑在服务器上?
本地运行 Puppeteer 爬虫有两个明显限制:本地 IP 容易被目标网站封禁,以及需要长期挂机影响电脑正常使用。将爬虫部署在香港 VPS 上,可以获得稳定的海外 IP、7×24 小时不间断运行,以及更快访问海外目标网站的网络优势。
一、安装系统依赖
Puppeteer 运行 Headless Chrome 需要一系列系统库,Ubuntu 上的完整依赖安装:
sudo apt update
sudo apt install -y \
ca-certificates \
fonts-liberation \
libasound2 \
libatk-bridge2.0-0 \
libatk1.0-0 \
libc6 \
libcairo2 \
libcups2 \
libdbus-1-3 \
libexpat1 \
libfontconfig1 \
libgbm1 \
libgcc1 \
libglib2.0-0 \
libgtk-3-0 \
libnspr4 \
libnss3 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libstdc++6 \
libx11-6 \
libx11-xcb1 \
libxcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrandr2 \
libxrender1 \
libxss1 \
libxtst6 \
xdg-utils中文字体支持(抓取含中文页面时防止乱码):
sudo apt install -y fonts-wqy-zenhei fonts-wqy-microhei二、安装 Node.js 和 Puppeteer
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs -y
mkdir ~/scraper && cd ~/scraper
npm init -y
npm install puppeteer三、基础爬虫脚本
nano scraper.jsconst puppeteer = require('puppeteer');
async function scrape(url) {
const browser = await puppeteer.launch({
headless: 'new',
args: [
'--no-sandbox', // 服务器环境必须加
'--disable-setuid-sandbox',
'--disable-dev-shm-usage', // 防止内存不足崩溃
'--disable-gpu',
'--no-first-run',
'--no-zygote',
'--single-process', // 低内存 VPS 节省内存
'--disable-extensions',
]
});
try {
const page = await browser.newPage();
// 设置 User-Agent(模拟正常浏览器)
await page.setUserAgent(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
// 设置视口大小
await page.setViewport({ width: 1280, height: 800 });
// 访问页面
await page.goto(url, {
waitUntil: 'networkidle2',
timeout: 30000
});
// 等待特定元素加载
await page.waitForSelector('body', { timeout: 10000 });
// 提取数据
const data = await page.evaluate(() => {
return {
title: document.title,
url: window.location.href,
content: document.body.innerText.substring(0, 500),
};
});
console.log(JSON.stringify(data, null, 2));
return data;
} finally {
await browser.close();
}
}
// 使用示例
scrape('https://example.com').catch(console.error);node scraper.js四、内存优化(低内存 VPS 关键配置)
Headless Chrome 每个实例约占用 100–300MB 内存,低配 VPS 需要特别注意:
# 设置页面内存限制
await page.setCacheEnabled(false); // 禁用缓存节省内存
# 拦截不必要的资源(图片、字体、CSS),加快加载并节省内存
await page.setRequestInterception(true);
page.on('request', (req) => {
const blocked = ['image', 'stylesheet', 'font', 'media'];
if (blocked.includes(req.resourceType())) {
req.abort();
} else {
req.continue();
}
});配置 Swap 空间防止 OOM:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab五、部署为定时任务
sudo nano /etc/systemd/system/scraper.service[Unit]
Description=Web Scraper
After=network.target
[Service]
Type=oneshot
User=youruser
WorkingDirectory=/home/youruser/scraper
ExecStart=/usr/bin/node scraper.js
StandardOutput=append:/var/log/scraper.log
StandardError=append:/var/log/scraper.logsudo nano /etc/systemd/system/scraper.timer[Unit]
Description=Run scraper every hour
[Timer]
OnBootSec=5min
OnUnitActiveSec=1h
[Install]
WantedBy=timers.targetsudo systemctl daemon-reload
sudo systemctl enable scraper.timer
sudo systemctl start scraper.timer总结
香港 VPS 部署 Puppeteer 爬虫的关键步骤:安装 Chrome 系统依赖 → 配置 --no-sandbox 等服务器必要参数 → 拦截无用资源节省内存 → 配置 Swap 防止 OOM → 用 systemd timer 实现定时运行。香港节点访问海外目标网站速度快,IP 相对干净,是爬虫部署的理想选择。
IDC.Net 香港云服务器首月 10 元起,2G 内存可运行轻量爬虫,4G 内存可稳定运行多个并发 Chrome 实例,CN2 GIA 直连大陆,支付宝付款即可开通。