将Shifter与以下工具配合使用 Puppeteer
几分钟内通过 Shifter 住宅代理驱动真实 Chromium 实例。原生 --proxy-server 支持、page.authenticate()、按标签页地理定向及完整集群扩展——无需额外插件。
快速入门
安装
npm install puppeteer 基本用法
import puppeteer from "puppeteer";
const browser = await puppeteer.launch({
args: ["--proxy-server=http://p.shifter.io:443"],
headless: "new",
});
const page = await browser.newPage();
await page.authenticate({
username: "customer-USERNAME-country-us-sid-123ABC",
password: "PASSWORD",
});
await page.goto("https://ipinfo.io/json");
console.log(await page.evaluate(() => document.body.textContent));
// {"ip": "154.16.xxx.xxx", "city": "New York", "country": "US", ...}
await browser.close(); 功能特性
示例
认证代理 + 粘性会话
通过在用户名中添加 `sid-XXX` 为整个浏览器会话固定住宅 IP。添加 `country-uk-city-london` 进行地理定向,添加 `ttl-300` 将该 IP 保持 300 秒。
import puppeteer from "puppeteer";
import { randomBytes } from "node:crypto";
const sid = randomBytes(4).toString("hex");
const browser = await puppeteer.launch({
args: [
"--proxy-server=http://p.shifter.io:443",
"--no-sandbox",
"--disable-blink-features=AutomationControlled",
],
headless: "new",
});
const page = await browser.newPage();
await page.authenticate({
username: `customer-USERNAME-country-uk-city-london-sid-${sid}-ttl-300`,
password: "PASSWORD",
});
await page.setUserAgent(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
);
// Multi-step flow — every page load reuses the same residential IP.
await page.goto("https://example.co.uk/login", { waitUntil: "networkidle0" });
await page.type("#email", "user@example.com");
await page.type("#password", "secret");
await page.click('button[type="submit"]');
await page.waitForNavigation({ waitUntil: "networkidle0" });
await page.goto("https://example.co.uk/dashboard");
const html = await page.content();
console.log(html.length, "bytes from dashboard");
await browser.close(); 按标签页地理定位
每个新标签页获得独立的代理凭据——让一个标签页抓取美国网站,另一个抓取日本网站,全部通过同一浏览器完成。
import puppeteer from "puppeteer";
const browser = await puppeteer.launch({
args: ["--proxy-server=http://p.shifter.io:443"],
headless: "new",
});
async function scrape(country: string, url: string) {
const page = await browser.newPage();
await page.authenticate({
username: `customer-USERNAME-country-${country}-sid-${country}-001`,
password: "PASSWORD",
});
await page.goto(url, { waitUntil: "domcontentloaded" });
const data = await page.evaluate(() => ({
title: document.title,
text: document.body.innerText.slice(0, 200),
}));
await page.close();
return { country, ...data };
}
const results = await Promise.all([
scrape("us", "https://www.example.com"),
scrape("jp", "https://www.example.jp"),
scrape("de", "https://www.example.de"),
scrape("br", "https://www.example.com.br"),
]);
console.log(results);
await browser.close(); Puppeteer Cluster(并行抓取)
在不占用大量内存的情况下并行扩展至数十个页面。通过 puppeteerOptions 传入代理,并在任务函数中按页面进行身份验证。
import { Cluster } from "puppeteer-cluster";
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_PAGE,
maxConcurrency: 10,
puppeteerOptions: {
args: ["--proxy-server=http://p.shifter.io:443"],
headless: "new",
},
monitor: true,
});
await cluster.task(async ({ page, data: url }) => {
await page.authenticate({
username: `customer-USERNAME-country-us-sid-${url.replace(/\W+/g, "").slice(0, 8)}`,
password: "PASSWORD",
});
await page.goto(url, { waitUntil: "networkidle0" });
const title = await page.title();
const html = await page.content();
return { url, title, length: html.length };
});
const urls = [
"https://example.com/category/laptops",
"https://example.com/category/phones",
"https://example.com/category/tablets",
// ... hundreds more
];
const results = await Promise.all(urls.map((url) => cluster.execute(url)));
console.log(results);
await cluster.idle();
await cluster.close(); 隐身模式 + 资源拦截
将 Shifter 与 puppeteer-extra 的隐身插件结合使用,并拦截图片、字体和媒体资源,在规避 Chrome 自动化标识的同时提升抓取速度 5 至 10 倍。
import puppeteer from "puppeteer-extra";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
puppeteer.use(StealthPlugin());
const browser = await puppeteer.launch({
args: [
"--proxy-server=http://p.shifter.io:443",
"--disable-blink-features=AutomationControlled",
],
headless: "new",
});
const page = await browser.newPage();
await page.authenticate({
username: "customer-USERNAME-country-us-city-newyork-sid-789GHI",
password: "PASSWORD",
});
// Block images, fonts, and media for faster page loads
await page.setRequestInterception(true);
page.on("request", (req) => {
const blocked = ["image", "font", "media", "stylesheet"];
blocked.includes(req.resourceType()) ? req.abort() : req.continue();
});
await page.goto("https://example.com", { waitUntil: "domcontentloaded" });
const products = await page.$$eval(".product", (els) =>
els.map((el) => ({
title: el.querySelector("h2")?.textContent?.trim(),
price: el.querySelector(".price")?.textContent?.trim(),
})),
);
console.log(products);
await browser.close(); 常见问题
关于将 Shifter 与 Puppeteer 搭配使用的常见问题。
Pass --proxy-server=http://p.shifter.io:443 to the browser launch args, then call page.authenticate({ username, password }) on each page before navigating. The same proxy applies to every tab and the credentials handle the basic-auth challenge transparently.
开始将Shifter与以下工具配合使用 Puppeteer
通过 Shifter 的 205M+ 住宅和 ISP 代理驱动无头 Chromium。支持原生 --proxy-server、按标签页地理定向、粘性会话以及完整的 Puppeteer-cluster 集成。