集成
将Shifter与以下工具配合使用 Node.js
在几分钟内将 Shifter 的住宅和 ISP 代理接入任何 Node.js 项目。与 axios、原生 fetch(Node 18+)、got、undici、Puppeteer 和 Playwright 无缝兼容。
快速入门
安装
npm install axios https-proxy-agent 基本用法
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
const proxyUrl =
"customer-USERNAME-country-us-sid-123ABC:PASSWORD@p.shifter.io:443";
const agent = new HttpsProxyAgent(proxyUrl);
const { data } = await axios.get("https://ipinfo.io/json", {
httpsAgent: agent,
proxy: false,
});
console.log(data);
// { ip: "154.16.xxx.xxx", city: "New York", country: "US", ... } 功能特性
开箱即用支持 axios、原生 fetch、got、undici、node-fetch 以及任何接受代理 URL 或 agent 的 HTTP 客户端
默认按请求轮换,使用`sid`实现粘性会话,使用`ttl-N`实现N秒定时固定
通过一行代理配置支持 Puppeteer 和 Playwright 无头浏览器
同一网关端点支持 HTTP、HTTPS 和 SOCKS5 协议
通过用户名参数在 195+ 个国家/地区进行地理定位 — 无需额外 SDK
兼容 TypeScript、ESM、CommonJS 以及从 14 起的所有 Node.js LTS 版本
示例
axios + 地理定向粘性会话
通过在用户名中添加 `sid` 将整个用户会话固定到同一个住宅 IP。添加 `country-uk`、`city-london` 或 `asn-7922` 进行地理定向。
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
import { randomBytes } from "node:crypto";
const sid = randomBytes(4).toString("hex");
const proxyUrl =
`customer-USERNAME-country-uk-city-london-sid-${sid}-ttl-300:` +
`PASSWORD@p.shifter.io:443`;
const client = axios.create({
httpsAgent: new HttpsProxyAgent(proxyUrl),
proxy: false,
timeout: 30_000,
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
},
});
const login = await client.post("https://example.com/login", {
email: "user@example.com",
password: "secret",
});
const dashboard = await client.get("https://example.com/dashboard");
const orders = await client.get("https://example.com/orders");
console.log(login.status, dashboard.status, orders.status); 原生 fetch(Node 18+)与 undici
如果您使用 Node 18 或更高版本,无需 axios,直接使用带有 undici ProxyAgent 的全局 fetch 即可。依赖最小化。
import { ProxyAgent, setGlobalDispatcher } from "undici";
const proxyAgent = new ProxyAgent({
uri: "http://p.shifter.io:443",
token: `Basic ${Buffer.from(
"customer-USERNAME-country-us-sid-456DEF:PASSWORD",
).toString("base64")}`,
});
setGlobalDispatcher(proxyAgent);
const r = await fetch("https://api.example.com/products?page=1");
const json = await r.json();
console.log(json); Puppeteer 与认证代理
通过 Shifter 路由 Chromium 并使用 page.authenticate() 进行认证。将 `sid` 与浏览器级 Cookie 结合,在整个爬取过程中保持稳定会话。
import puppeteer from "puppeteer";
const PROXY_HOST = "p.shifter.io";
const PROXY_PORT = 443;
const PROXY_USER = "customer-USERNAME-country-de-sid-789GHI";
const PROXY_PASS = "PASSWORD";
const browser = await puppeteer.launch({
args: [`--proxy-server=http://${PROXY_HOST}:${PROXY_PORT}`],
headless: "new",
});
const page = await browser.newPage();
await page.authenticate({ username: PROXY_USER, password: PROXY_PASS });
await page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
);
await page.goto("https://example.com", { waitUntil: "networkidle0" });
const title = await page.title();
const html = await page.content();
console.log(title, html.length, "bytes");
await browser.close(); Playwright(多浏览器)
Playwright 的启动选项原生支持代理,并可内联填写凭据。同样的配置适用于 Chromium、Firefox 和 WebKit。
import { chromium } from "playwright";
const browser = await chromium.launch({
proxy: {
server: "http://p.shifter.io:443",
username: "customer-USERNAME-country-fr-city-paris-sid-ABC123",
password: "PASSWORD",
},
});
const context = await browser.newContext({
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
});
const page = await context.newPage();
await page.goto("https://example.com");
const headlines = await page.locator(".headline").allTextContents();
console.log(headlines);
await browser.close(); 常见问题
常见问题
关于将 Shifter 与 Node.js 搭配使用的常见问题。
在 httpsAgent 选项中传入 https-proxy-agent(或用于普通 HTTP 的 http-proxy-agent),并设置 proxy: false 以使 axios 直接使用该代理。代理 URL 格式为 `http://USER:PASS@p.shifter.io:443`。同一个 axios 实例随后可在多个请求中复用。
立即开始
开始将Shifter与以下工具配合使用 Node.js
在 5 分钟内将 Shifter 的 205M+ 住宅和 ISP 代理添加到您的 Node.js 技术栈。支持按请求轮换、粘性会话以及完整的 Puppeteer / Playwright 集成。
免费试用 Shifter几分钟内完成设置,随时可取消。