注册
登录
 文档中心 产品介绍 开发指南 API接口 代码样例 使用帮助
Node.js使用Puppeteer调用Socks5代理IP的代码样例
站大爷 官方 2024-01-21 1598 浏览

温馨提示:

1.  http/https网页均可适用

2.  运行环境要求: node7.6.0或以上 + puppeteer

3.  安装puppeteer: npm i puppeteer

4.  puppeteer模块的socks代理暂不支持用户名密码的方式验证身份


// 引入puppeteer模块
const puppeteer = require('puppeteer');

// 要访问的目标网页
const url = 'http://example.com';

// 添加headers
const headers = {
	'Accept-Encoding': 'gzip'
};

// 代理服务器ip和端口
let proxy_ip = '159.138.141.125'
let proxy_port = 11918

(async ()=> {
	// 新建一个浏览器实例
	const browser = await puppeteer.launch({
		headless: false,  // 是否不显示窗口, 默认为true, 设为false便于调试
		args: [
			`--proxy-server=socks5h://${proxy_ip}:${proxy_port}`,
			'--no-sandbox',
			'--disable-setuid-sandbox'
		]
	});

	// 打开一个新页面
	const page = await browser.newPage();
	// 设置headers
	await page.setExtraHTTPHeaders(headers);

	// 访问目标网页
	await page.goto(url);

})();


立即注册站大爷用户,免费试用全部产品
立即注册站大爷用户,免费试用全部产品