其他分享
首页 > 其他分享> > cheerio 解析新浪博客目录列表

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();
      });
    }
  });
});

标签:fs,const,cheerio,列表,write,file,path,解析,writeStream
来源: https://www.cnblogs.com/zhuoss/p/15897938.html