为何选择 NODE.JS + PROXY POLAND
Node.js 是服务器端自动化、API 爬取和无头浏览器自动化的首选运行时。通过 Proxy Poland 的 4G 手机代理路由您的 Node.js 请求,您将获得真实的手机运营商 IP,可绕过反机器人系统和地理限制。
设置说明
安装 HTTP 客户端库
安装 axios 或使用内置 https 模块:
npm install axios # For SOCKS5 support: npm install socks-proxy-agent # For HTTPS proxy: npm install https-proxy-agent
获取代理凭据
在 proxypoland.com 注册并获取您的代理 IP、端口、用户名和密码。
使用 Axios 配置 HTTP 代理
通过代理路由 Axios 请求:
const axios = require('axios');
const response = await axios.get('https://httpbin.org/ip', {
proxy: {
host: 'proxy-ip',
port: 8080,
auth: {
username: 'your-username',
password: 'your-password',
},
protocol: 'http',
},
});
console.log(response.data);
// Should show Polish mobile IP使用原生 https 模块配置
使用 Node.js 内置 https 带 SOCKS5 代理:
const { SocksProxyAgent } = require('socks-proxy-agent');
const https = require('https');
const agent = new SocksProxyAgent(
'socks5://username:password@proxy-ip:port'
);
const options = {
hostname: 'httpbin.org',
path: '/ip',
agent,
};
https.get(options, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => console.log(JSON.parse(data)));
});与 node-fetch 一起使用
配置带代理代理的 node-fetch:
const fetch = require('node-fetch');
const { HttpsProxyAgent } = require('https-proxy-agent');
const agent = new HttpsProxyAgent(
'http://username:password@proxy-ip:port'
);
const response = await fetch('https://httpbin.org/ip', { agent });
const data = await response.json();
console.log(data);添加 IP 轮换
使用轮换 API 在爬取会话之间轮换 IP:
const axios = require('axios');
async function rotateIP() {
const response = await axios.get(
`http://proxy-ip:port/rotate`,
{ auth: { username: 'user', password: 'pass' } }
);
console.log('New IP:', response.data);
return response.data;
}
// Rotate between scraping tasks
await rotateIP();
const result = await axios.get('https://target-site.com', { proxy: proxyConfig });验证您的 IP
检查您的请求是否通过代理路由:
const axios = require('axios');
const response = await axios.get('https://httpbin.org/ip', {
proxy: {
host: 'proxy-ip',
port: 8080,
auth: { username: 'user', password: 'pass' },
},
});
console.log(response.data.origin);
// Should show Polish mobile carrier IP专业提示
使用 https-proxy-agent 或 socks-proxy-agent 与大多数 Node.js HTTP 客户端兼容
使用 p-retry 或指数退避实施重试逻辑以进行弹性爬取
使用 async/await 和 Promise.all 进行并发代理路由请求
在爬取批次之间轮换 IP 以避免基于 IP 的封禁
使用 user-agents npm 包设置真实的 User-Agent 标头
适用于以下场景
FAQ
哪个 Node.js HTTP 库最适合手机代理?+
Axios 因其简单的代理配置而最受欢迎。对于原生使用,https-proxy-agent 和 socks-proxy-agent 与 Node.js 内置模块配合使用。got 和 node-fetch 也支持代理代理。
如何在 Node.js 中使用 SOCKS5 代理?+
安装 socks-proxy-agent(npm install socks-proxy-agent),并将其作为代理参数传递给您的 HTTP 客户端。代理 URL 格式为:socks5://用户:密码@主机:端口。
如何在 Node.js 中处理代理认证?+
对于 Axios,使用 auth.username 和 auth.password 设置代理配置对象。对于基于代理的客户端,在代理 URL 中包含凭据:http://用户名:密码@主机:端口。
我可以在 Node.js 中将这些代理与 Playwright 或 Puppeteer 一起使用吗?+
可以。将 --proxy-server=socks5://主机:端口 作为启动参数传递。然后使用 page.authenticate() 提供凭据。请参阅我们专用的 Puppeteer 集成指南。
如何在 Node.js 中实施 IP 轮换?+
在爬取任务之间调用轮换端点(在您的代理上 GET /rotate)。API 返回新的 IP 地址。您可以使用简单的 setInterval 或按请求触发来自动化此过程。