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` を、その IP を 300 秒間保持するには `ttl-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(); タブごとのジオターゲティング
新しいページごとに独自のプロキシ認証情報が割り当てられます。1つのタブで米国サイトをスクレイピングしながら、別のタブで日本のサイトをスクレイピングすること、すべて1つのブラウザを通じて行えます。
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.
はい、最新のPuppeteer(headless: 'new'使用時)で使用できます。レガシーの`headless: true`モードも対応しています。Chromiumバージョン105より古いバージョンを使用している場合は、資格情報を注入するChrome拡張機能にフォールバックする必要がある場合がありますが、最近のPuppeteerリリースではpage.authenticateが正常に動作します。
プロキシのユーザー名にセッションIDを追加してください。例:`customer-USERNAME-country-us-sid-123ABC`。そのユーザー名で認証するすべてのページは同じレジデンシャルIPを共有します。`ttl-N`を追加すると、そのIPをN秒まで固定できます。
はい。各newPage()呼び出しは、異なるShifterユーザー名で認証できます。一方のタブには`country-us`、別のタブには`country-jp`といった具合です。ブラウザレベルの状態(Cookie、キャッシュ)は共有されますが、出口IPはタブごとに変わるため、ローカライズされたコンテンツを並列でスクレイピングするのに最適です。
はい。ステルスプラグインがChromeの自動化フィンガープリントにパッチを当て、ShifterがレジデンシャルIPを処理します。この二つはきれいに組み合わせられます。puppeteer-extraとステルスプラグインをインストールし、通常どおり起動時にプロキシを設定してください。
Cluster.launch呼び出し時にpuppeteerOptionsで--proxy-server引数を渡し、cluster.taskコールバック内でpage.authenticateを呼び出します。ワーカー間でIPを共有しない並列スクレイピングのために、タスクごとに一意のsidと組み合わせてください。
でShifterを使い始める Puppeteer
ShifterのレジデンシャルおよびISP Proxy 205M+を通じてヘッドレスChromiumを操作できます。ネイティブの--proxy-server、タブごとのジオターゲティング、スティッキーセッション、Puppeteer-cluster完全サポートに対応しています。