cheerio 解析新浪博客目录列表
2022/2/15 23:15:05
本文主要是介绍cheerio 解析新浪博客目录列表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
正序目录
- 解析提取手机版新浪博客网页的目录,然后存成markdown文件
const fs = require('fs'); const cheerio = require('cheerio'); let path = `./html`; fs.readdir(path, function (err, files) { files.forEach((file) => { console.log(file); if (file.split('.')[1] === 'txt') { fs.readFile(path + '/' + file, 'utf-8', (err, data) => { const $ = cheerio.load(data); // const writeStream = fs.createWriteStream(path + '/' + file, 'utf-8'); const writeStream = fs.createWriteStream( path + '/目录/' + '新浪博客目录.txt', 'utf-8' ); $('#pl-home-bloglist > article > ul>li').each((data, ele) => { let title = $(ele).find('h2').html(); let url = $(ele).find('a').attr('data-link'); writeStream.write('['); writeStream.write(title); writeStream.write(']'); writeStream.write('('); writeStream.write(url); writeStream.write(')'); writeStream.write('\n'); writeStream.write('\n'); console.log(title); console.log(url); }); writeStream.end(); }); } }); });
倒序目录
- 和上面的效果一样,只不过是倒序的
const fs = require('fs'); const cheerio = require('cheerio'); let path = `./html`; fs.readdir(path, function (err, files) { files.forEach((file) => { console.log(file); if (file.split('.')[1] === 'txt') { fs.readFile(path + '/' + file, 'utf-8', (err, data) => { const $ = cheerio.load(data); // const writeStream = fs.createWriteStream(path + '/' + file, 'utf-8'); const writeStream = fs.createWriteStream( path + '/目录/' + '新浪博客目录.txt', 'utf-8' ); $($('#pl-home-bloglist > article > ul>li').get().reverse()).each( (data, ele) => { let title = $(ele).find('h2').html(); let url = $(ele).find('a').attr('data-link'); writeStream.write('['); writeStream.write(title); writeStream.write(']'); writeStream.write('('); writeStream.write(url); writeStream.write(')'); writeStream.write('\n'); writeStream.write('\n'); console.log(title); console.log(url); } ); writeStream.end(); }); } }); });
这篇关于cheerio 解析新浪博客目录列表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26MATLAB 中 A(7)=[];什么意思?-icode9专业技术文章分享
- 2024-11-26UniApp 中如何实现使用输入法时保持页面列表不动的效果?-icode9专业技术文章分享
- 2024-11-26在 UniApp 中怎么实现输入法弹出时禁止页面向上滚动?-icode9专业技术文章分享
- 2024-11-26WebSocket是什么,怎么使用?-icode9专业技术文章分享
- 2024-11-26页面有多个ref 要动态传入怎么实现?-icode9专业技术文章分享
- 2024-11-26在 UniApp 中实现一个底部输入框的常见方法有哪些?-icode9专业技术文章分享
- 2024-11-26RocketMQ入门指南:搭建与使用全流程详解
- 2024-11-26RocketMQ入门教程:轻松搭建与使用指南
- 2024-11-26手写RocketMQ:从入门到实践的简单教程
- 2024-11-25【机器学习(二)】分类和回归任务-决策树(Decision Tree,DT)算法-Sentosa_DSML社区版