nodejs使用爬蟲代理的方案

/ 要訪問的目標頁面
const targetUrl = "http://httpbin.org/ip";

const urlParsed = url.parse(targetUrl);

// 代理服務器
const proxyHost = "t.16yun.cn";
const proxyPort = "36600";

// 生成一個隨機 proxy tunnel
var seed = 1;
function random() {
var x = Math.sin(seed++) 10000;
return x - Math.floor(x);
}
const tunnel = random()
100;

// 代理驗證信息
const proxyUser = "username";
const proxyPass = "password";

const base64 = new Buffer.from(proxyUser + ":" + proxyPass).toString("base64");

const options = {
host: proxyHost,
port: proxyPort,
path: targetUrl,
method: "GET",
headers: {
"Host": urlParsed.hostname,
"Proxy-Tunnel": tunnel,
"Proxy-Authorization" : "Basic " + base64
}
};

http.request(options, function (res) {
console.log("got response: " + res.statusCode);
res.pipe(process.stdout);
}).on("error", function (err) {
console.log(err);
}).end();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章