文章目录

最近在研究爬虫使用的是request模块,在访问被墙网址时需要为它添加代理功能,在npm上找到了socks5-http-client模块可以使用socks5代理,如果需要访问https可以使用socks5-https-client模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var agent = require('socks5-http-client/lib/Agent');
var request = require('request');
request.get({
url: 'http://wwww.google.com',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36'
},
agentClass: agent,
agentOptions: {
socksHost: '127.0.0.1', // socks5代理服务器ip
socksPort: 1080 // socks5代理服务器端口
}
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
});

文章目录