nodeJS 学习--用cheerio 制作超简单的网络爬虫

前置: 
1.安装好了nodejs
2.npm i cheerio
3.jquery选择器基础知识

var
https = require('https') var cheerio = require('cheerio')
// 简书首页
var url = "https://www.jianshu.com/" function filterSection(html) { //加载爬到的html结构 var $ = cheerio.load(html) var noteList = $('.note-list') var sectionData = [] noteList.each(function(item) { var content = $(this).find('.content')
// note 作者
var author = content.find('.author').find('.info').text()
// note title
var title = content.find('.title').text()
// note 内容摘要
var abstract = content.find('.abstract').text() var noteListData = { author: author, title: title, abstract: abstract } sectionData.push(noteListData) }) return sectionData } function print(sectionData) { sectionData.forEach((item) => { var author = item.author, title = item.title, abstract = item.abstract; console.log(author + '\n' + title + '\n' + abstract + '\n') }) } https.get(url, (res) => { var html = '' res.on('data', (data) => { html += data }) res.on('end', () => { var sectionData = filterSection(html)
print(sectionData) }).on(
'error', () => { console.log('错误') }) })

 

转载于:https://www.cnblogs.com/footstep/p/8275084.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值