需要执行 npm install 后,再运行node testcheerio.js即可
const https = require('https');const fs = require('fs');const cheerio = require('cheerio');const url = 'https://segmentfault.com/';https.get(url, (res) => { let html = ''; res.on('data', (data) => { html = data; }); res.on('end', () => { getPageTitle(html); });}).on('error', () => { console.log('获取网页信息错误');});function getPageTitle(html) { const $ = cheerio.load(html); let chapters = $('.news__item-title'); let data = []; let index = 0; let fileName = 'pageTitle.txt'; for (let i = 0; i < chapters.length; i ) { let chapterTitle = $(chapters[i]).find('a').text().trim(); index ; data.push(`\n${index}, ${chapterTitle}`); } fs.writeFile(fileName, data, 'utf8', (err) => { if (err) { console.log('fs文件系统创建新文件失败', err); } console.log(`已成功将获取到的标题放入新文件${fileName}文件中`) })}
评论