使用Node.js通过API接口获取代理IP的代码样例
站大爷
官方
2024-01-20
1448 浏览
温馨提示:
1. API提取链接为站大爷控制台实例管理里生成,请勿直接复制样例使用
const http = require('http'); // 引入内置http模块
let api_url = 'http://www.***.com/ShortProxy/GetIP/?api=1234567890&akey=8a17ca305f683620&count=1×pan=3&type=1'; // 要访问的目标网页
// 采用gzip压缩, 使速度更快
let options = {
"headers" : {
"Accept-Encoding": "gzip"
}
};
// 发起请求
http.get(api_url, options, (res) => {
// 若有gzip压缩, 则解压缩再输出
if (res.headers['content-encoding'] && res.headers['content-encoding'].indexOf('gzip') != -1) {
let zlib = require('zlib');
let gunzipStream = zlib.createGunzip();
res.pipe(gunzipStream).pipe(process.stdout);
} else {
// 无gzip压缩,直接输出
res.pipe(process.stdout);
}
}).on("error", (err) => {
// 错误处理
console.log("Error: " + err.message);
})