获取网页内容(httprequestsuperagent等)html
筛选网页信息(cheerio)node
输出或存储信息(consolefsmongodbmysql等)mysql
var request = require('request'); // 经过 GET 请求来读取 http://cnodejs.org/ 的内容 request('http://cnodejs.org/', function (error, response, body) { if (!error && response.statusCode == 200) { // 输出网页内容 console.log(body); } });
若是是其余的请求方法,或者须要指定请求头等信息,能够在第一个参数中传入一个对象来 指定,好比:jquery
var request = require('request'); request({ url: 'http://cnodejs.org/', // 请求的URL method: 'GET', // 请求方法 headers: { // 指定请求头 'Accept-Language': 'zh-CN,zh;q=0.8', // 指定 Accept-Language 'Cookie': '__utma=4454.11221.455353.21.143;' // 指定 Cookie } }, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // 输出网页内容 } });
cheerio 是一个 jQuery Core 的子集,其实现了 jQuery Core 中浏览器无关的 DOM 操做 API,如下是一个简单的示例:sql
var cheerio = require('cheerio'); // 经过 load 方法把 HTML 代码转换成一个 jQuery 对象 var $ = cheerio.load('<h2 class="title">Hello world</h2>'); // 能够使用与 jQuery 同样的语法来操做 $('h2.title').text('Hello there!'); $('h2').addClass('welcome'); console.log($.html()); // 将输出 <h2 class="title welcome">Hello there!</h2>
mysql 模块内置了链接池机制,如下是一个简单的使用示例:mongodb
var mysql = require('mysql'); // 建立数据库链接池 var pool = mysql.createPool({ host: 'localhost', // 数据库地址 user: 'root', // 数据库用户 password: '', // 对应的密码 database: 'example', // 数据库名称 connectionLimit: 10 // 最大链接数,默认为10 }); // 在使用 SQL 查询前,须要调用 pool.getConnection() 来取得一个链接 pool.getConnection(function(err, connection) { if (err) throw err; // connection 即为当前一个可用的数据库链接 });
jquery选择器总结 https://www.cnblogs.com/xiaxuexiaoab/p/7091527.html nodejs爬虫 https://www.cnblogs.com/xiaxuexiaoab/p/7124956.html
欢迎评论数据库