使用http.get 可以爬取网页中的指定的数据
说起到爬取,就会想起 node的一个插件 cheerio
安装
npm install cheerio 或 cnpm install cheerio
简单使用
引入cheerio模块
const cheerio = require('cheerio')
加载HTML字符串 (就是你想获取数据页面的数据)
const $ = cheerio.load('<h2 class="title">Hello world</h2>')
设置Text
$('h2.title').text('Hello there!')
添加class
$('h2').addClass('welcome')
获取完整HTML
$.html()
//=> <html><head></head><body>
// <h2 class="title welcome">Hello there!</h2></body></html>
感觉就像在用jquery()
let http = require("http")
http.get("http://www.baidu.com/2001.php",(res)=>{
if(err){
console.log(err)
}else{
console.log("成功")
var html = "";
res.on("data",(chunck)=>{
html+=chunck;}) //获取的数据永html保存
}
res.on("end",()=>{ //获取的全部内容
console.log(html) //得到爬取的数据
})
} )