node 爬虫mysql_nodejs爬虫 http,cheerio,mysql模块

nodejs相关模块

获取网页内容(httprequestsuperagent等)

筛选网页信息(cheerio)

输出或存储信息(consolefsmongodbmysql等)

1、使用 request 模块来获取网页内容

var request = require('request');

// 通过 GET 请求来读取 http://cnodejs.org/ 的内容

request('http://cnodejs.org/', function (error, response, body) {

if (!error && response.statusCode == 200) {

// 输出网页内容

console.log(body);

}

});

如果是其他的请求方法,或者需要指定请求头等信息,可以在第一个参数中传入一个对象来 指定,比如:

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) // 输出网页内容

}

});

2、使用 cheerio 模块来提取网页中的数据

cheerio 是一个 jQuery Core 的子集,其实现了 jQuery Core 中浏览器无关的 DOM 操作 API,以下是一个简单的示例:

var cheerio = require('cheerio');

// 通过 load 方法把 HTML 代码转换成一个 jQuery 对象

var $ = cheerio.load('

Hello world

');

// 可以使用与 jQuery 一样的语法来操作

$('h2.title').text('Hello there!');

$('h2').addClass('welcome');

console.log($.html());

// 将输出

Hello there!

3、使用 mysql 模块来将数据储存到数据库

mysql 模块内置了连接池机制,以下是一个简单的使用示例:

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

欢迎评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值