引入
cheerio 模块 ,node.js 版的jquery
https 模块,封装了一个http服务器和一个简易的http客户端
iconv-lite 模块, 可能需要解决的不同页面编码不一样 utf8,gb2312
var cheerio=require('cheerio')
var https=require('https')
var iconv=require('iconv-lite')
var path=require('path')
var url='https://www.jianshu.com/p/79adf1f8ebc7'
https.get(url,(res)=>{
var chunks=[]
res.on('data',(chunk)=>{
chunks.push(chunk)
})
res.on('end',(res)=>{
var titles=[]
var html=iconv.decode(Buffer.concat(chunks),'utf8')
var $=cheerio.load(html,{decodeEntities:false})
$('p').each((idx,element)=>{
var $element=$(element)
titles.push({
title:$element.text()
})
})
console.log(titles)
})
})

准备去爬 https://www.jianshu.com/p/79adf1f8ebc7这篇文章下面的评论的,结果爬出来的是整个文章
网友评论