const https = require('https'); const http = require('http'); const url = require('url'); const cheerio = require('cheerio'); const args = process.argv.slice(2); const num = args[0]; var optionsBD = { host: 'www.baidu.com', port: 80, path: '/s?wd=' + num, method: 'GET' }; var options360 = { host: 'www.so.com', port: 443, path: '/s?q=' + num, method: 'GET' }; if (!/^\d+$/.test(num)) { console.log(`参数错误,请输入正确电话号码或查询平台。\r\n例如:node phoneMark.js 15949566632`); process.exit(0); } function request(protocol, options, cb) { return new Promise((resolve, reject) => { var req = protocol.request(options, (res) => { var html = ''; res.setEncoding('utf8'); res.on('data', (chunk) => { html += chunk; }); res.on('end', () => { resolve(cb(html)); }); }); req.on('error', (err) => { reject(err); }); req.end(); }); } Promise .all([request(http, optionsBD, AnalysizeByBD), request(https, options360, AnalysizeBy360)]) .then((vals) => { summary(vals); }, (err) => { console.log(err); }); function summary(vals) { if ((/\d+/g).test(vals[0])) { console.log(`号码${num}:为骚扰号码,分别被百度、360标记${vals[0]}、${vals[1]}次`); } else { console.log(`号码${num}:${vals[0]}`); } } function AnalysizeByBD(html) { var $ = cheerio.load(html); var arr = $('.op_fraudphone_word').text().trim().replace(/\s+/g, "").match(/(\d+)/g) return arr ? arr[0] : $('.op_mobilephone_r').find('span').eq(1).text().replace(/\s*/g, ""); } function AnalysizeBy360(html) { var $ = cheerio.load(html); return $('.mohe-tips').find('b').text(); }
执行结果如下:
E:\developmentdocument\nodejsdemo>node number.js 15656069453
号码15656069453:为骚扰号码,分别被百度、360标记12、8502次
http模块便介绍到这。