用Nodejs實現一個簡單的爬蟲功能。(ES6標準)
Nodejs版本:v10.11.0
依賴模組:express,superagent,cheerio
程式碼:
const express = require('express'); const superagent = require('superagent'); const cheerio = require('cheerio');
const app = express();
app.get('/', function (req, res, next) { superagent.get('https://cnodejs.org/') .end(function (err, sres) { if (err) { return next(err); } const $ = cheerio.load(sres.text); let items = []; $('#topic_list .cell').each(function (idx, element) { const $element = $(element).find('.topic_title').eq(0); const $author = $(element).find('.user_avatar img').eq(0); items.push({ title: $element.attr('title'), href: $element.attr('href'), author: $author.attr('title') }); }); res.send(items); }); });
app.listen(3000, function(req,res){ console.log('app is running at port 3000'); })