用request库可以很方便这种任务,下面是我写的一个获取可用代理列表 + 用代理访问目标网站的Demo
注意 如果下载国内或者国外的文件 应该使用对应地域稳定的IP连续下载

var request = require("request");
var iconv = require('iconv-lite');
var Promise = require("bluebird");


function getProxyList() {
    var apiURL = 'http://www.66ip.cn/mo.php?sxb=&tqsl=100&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=http%3A%2F%2Fwww.66ip.cn%2F%3Fsxb%3D%26tqsl%3D100%26ports%255B%255D2%3D%26ktip%3D%26sxa%3D%26radio%3Dradio%26submit%3D%25CC%25E1%2B%2B%25C8%25A1';

    return new Promise((resolve, reject) => {
        var options = {
            method: 'GET',
            url: apiURL,
            gzip: true,
            encoding: null,
            headers: {
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4',
                'User-Agent': 'Mozilla/8.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
                'referer': 'http://www.66ip.cn/'
            },

        };

        request(options, function (error, response, body) {


            try {

                if (error) throw error;

                if (/meta.*charset=gb2312/.test(body)) {
                    body = iconv.decode(body, 'gbk');
                }

                var ret = body.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,4}/g);


                resolve(ret);

            } catch (e) {
                return reject(e);
            }


        });
    })
}


getProxyList().then(function (proxyList) {

    var targetOptions = {
        method: 'GET',
        url: 'http://ip.chinaz.com/getip.aspx',
        timeout: 8000,
        encoding: null,
    };

    //这里修改一下,变成你要访问的目标网站
    proxyList.forEach(function (proxyurl) {

        console.log(`testing ${proxyurl}`);

        targetOptions.proxy = 'http://' + proxyurl;
        request(targetOptions, function (error, response, body) {
            try {
                if (error) throw error;


                body = body.toString();

                console.log(body);

                eval(`var ret = ${body}`);


                if (ret) {
                    console.log(`验证成功==>> ${ret.address}`);
                }
            } catch (e) {
                // console.error(e);
            }


        });

    });
}).catch(e => {
    console.log(e);
})

感谢https://segmentfault.com/q/1010000008196143/a-1020000008200295

Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例

每种语言都有自己的优势,互相结合起来各取所长程序执行起来效率更高或者说哪种实现方式较简单就用哪个,nodejs是利用子进程来调用系统命令或者文件,文档见...

阅读全文

nodejs通过代理(proxy)发送http请求(request)

代码未经测试 var http = require('http') var opt = { host:'这里放代理服务器的ip或者域名', port:'这里放代理服务器的端口号', method:'POST',//...

阅读全文

nodejs 使用socket.io与网页实时数据交互

首先我们需要安装socket模块 安装命令: npm install socket.io 编辑前台页面:index.html <!DOCTYPE html>       <html>       <head lang="...

阅读全文

评论已经关闭。