cheerio 解析新浪博客目录列表

2022/2/15 23:15:05

本文主要是介绍cheerio 解析新浪博客目录列表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

正序目录

  1. 解析提取手机版新浪博客网页的目录,然后存成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();
      });
    }
  });
});

倒序目录

  1. 和上面的效果一样,只不过是倒序的
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 解析新浪博客目录列表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程