集成

将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();

功能特性

原生代理支持,凭据内联传入——无需附加扩展或调整浏览器配置
相同配置适用于所有支持平台上的 Chromium、Firefox 和 WebKit
按上下文代理配置,可在同一浏览器实例中混合使用多个国家
Node、Python、Java 和 .NET 使用统一 API——每种语言中代理字典结构相同
通过用户名参数在 195+ 个国家/地区进行地理定向 — country、region、city、ASN
默认按请求轮换,使用`sid`实现粘性会话,使用`ttl-N`实现N秒定时固定

示例

按上下文地理定向(单浏览器,多地区)

每个浏览器上下文可拥有独立代理。在同一浏览器实例中启动美国、英国和日本上下文——这是 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 的结构完全相同。凭据内联传入——无需扩展,且开箱即支持无头模式。

立即开始

开始将Shifter与以下工具配合使用 Playwright

通过 Shifter 的 205M+ 住宅和 ISP 代理驱动 Chromium、Firefox 和 WebKit——凭据内联,无需扩展。支持 Node、Python、Java 和 .NET。

免费试用 Shifter几分钟内完成设置,随时可取消。