将Shifter与以下工具配合使用 Playwright
通过 Shifter 的住宅和 ISP 代理运行 Chromium、Firefox 和 WebKit,凭据内联填写,无需侧载扩展。在 Node、Python、Java 和 .NET 中均提供一流的代理支持。
快速入门
安装
npm install playwright 基本用法
import { chromium } from "playwright";
const browser = await chromium.launch({
proxy: {
server: "http://p.shifter.io:443",
username: "customer-USERNAME-country-us-sid-123ABC",
password: "PASSWORD",
},
});
const page = await browser.newPage();
await page.goto("https://ipinfo.io/json");
console.log(await page.textContent("body"));
// {"ip": "154.16.xxx.xxx", "city": "New York", "country": "US", ...}
await browser.close(); 功能特性
示例
按上下文地理定向(单浏览器,多地区)
每个浏览器上下文可拥有独立代理。在同一浏览器实例中启动美国、英国和日本上下文——这是 Playwright 用于并行本地化抓取的核心特性。
import { chromium } from "playwright";
const browser = await chromium.launch();
async function makeContext(country: string, sid: string) {
return browser.newContext({
proxy: {
server: "http://p.shifter.io:443",
username: `customer-USERNAME-country-${country}-sid-${sid}`,
password: "PASSWORD",
},
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
});
}
const [us, uk, jp] = await Promise.all([
makeContext("us", "us-001"),
makeContext("uk", "uk-001"),
makeContext("jp", "jp-001"),
]);
const [usPage, ukPage, jpPage] = await Promise.all([
us.newPage(),
uk.newPage(),
jp.newPage(),
]);
await Promise.all([
usPage.goto("https://www.example.com"),
ukPage.goto("https://www.example.co.uk"),
jpPage.goto("https://www.example.jp"),
]);
console.log({
us: await usPage.title(),
uk: await ukPage.title(),
jp: await jpPage.title(),
});
await browser.close(); 多浏览器(Chromium / Firefox / WebKit)
三种引擎使用相同的代理配置。适用于跨浏览器抓取,或针对特定目标选择最不易触发机器人检测的引擎。
import { chromium, firefox, webkit, type BrowserType } from "playwright";
const proxy = {
server: "http://p.shifter.io:443",
username: "customer-USERNAME-country-de-city-berlin-sid-456DEF",
password: "PASSWORD",
};
async function visit(engine: BrowserType, label: string) {
const browser = await engine.launch({ proxy });
const page = await browser.newPage();
await page.goto("https://example.de");
const title = await page.title();
await browser.close();
return { label, title };
}
const results = await Promise.all([
visit(chromium, "chromium"),
visit(firefox, "firefox"),
visit(webkit, "webkit"),
]);
console.log(results); Playwright Test 套件
在 playwright.config.ts 中一次性配置 Shifter,使所有测试均通过代理路由。使用环境变量存储凭据,避免将其提交到源代码中。
// playwright.config.ts
import { defineConfig } from "@playwright/test";
export default defineConfig({
testDir: "./tests",
workers: 4,
use: {
proxy: {
server: "http://p.shifter.io:443",
username: process.env.SHIFTER_USER!, // e.g. customer-USERNAME-country-us-sid-test
password: process.env.SHIFTER_PASS!,
},
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
viewport: { width: 1920, height: 1080 },
ignoreHTTPSErrors: false,
},
});
// tests/products.spec.ts
import { test, expect } from "@playwright/test";
test("US homepage renders", async ({ page }) => {
await page.goto("https://example.com");
await expect(page.getByRole("heading", { level: 1 })).toBeVisible();
});
test("Product page parses", async ({ page }) => {
await page.goto("https://example.com/products/123");
const price = await page.locator(".price").textContent();
expect(price).toMatch(/^\$\d+/);
}); Python:使用粘性会话的 chromium.launch
Playwright 在各语言中完全一致。Python 中使用相同的字典形式代理配置,其余 API 与 Node 版本一一对应。
# pip install playwright
# playwright install chromium
import asyncio
import secrets
from playwright.async_api import async_playwright
async def main():
sid = secrets.token_hex(4)
async with async_playwright() as p:
browser = await p.chromium.launch(
proxy={
"server": "http://p.shifter.io:443",
"username": f"customer-USERNAME-country-fr-city-paris-sid-{sid}-ttl-300",
"password": "PASSWORD",
}
)
context = await browser.new_context(
user_agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36",
)
page = await context.new_page()
# Multi-step flow — same residential IP across every navigation.
await page.goto("https://example.fr/login", wait_until="networkidle")
await page.fill("#email", "user@example.com")
await page.fill("#password", "secret")
await page.click("button[type=submit]")
await page.wait_for_url("**/dashboard")
rows = await page.locator(".order-row").all_text_contents()
print(f"Found {len(rows)} orders")
await browser.close()
asyncio.run(main()) 常见问题
关于将 Shifter 与 Playwright 搭配使用的常见问题。
在浏览器启动选项中传入包含 `server`、`username` 和 `password` 的 `proxy` 字段。Chromium、Firefox 和 WebKit 的结构完全相同。凭据内联传入——无需扩展,且开箱即支持无头模式。
可以。将 `proxy` 传入 `browser.newContext()`,该上下文将使用与其他上下文不同的代理。这是在单个进程中并行抓取多个国家的最简洁方式——比启动多个浏览器节省大量内存。
支持。相同的代理配置适用于三种引擎。当目标网站存在针对 Chrome 的机器人检测时,Firefox 和 WebKit 非常有用——Playwright 是少数可以一行代码切换引擎的工具之一。
在代理用户名中添加会话 ID,例如 `customer-USERNAME-country-us-sid-123ABC`。该上下文中的所有导航、请求和标签页都将复用同一住宅 IP。添加 `ttl-N` 可将其固定最多 N 秒。
可以。在 `playwright.config.ts` 中设置 `use.proxy` 字段。所有未覆盖该配置的测试都将继承 Shifter 代理。使用环境变量存储凭据,避免其出现在源代码管理中。
支持——Playwright 提供 Python、Java 和 .NET 的官方客户端。代理配置在所有语言中使用相同结构(server、username、password)。本页示例展示了 Node 和 Python,但相同配置适用于所有语言。
开始将Shifter与以下工具配合使用 Playwright
通过 Shifter 的 205M+ 住宅和 ISP 代理驱动 Chromium、Firefox 和 WebKit——凭据内联,无需扩展。支持 Node、Python、Java 和 .NET。