We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
最近因为工作中使用node.js来开发工具,所以想到了分享下之前写的简单爬虫!
这里我就拿正妹的博客做演示了……
var express = require('express'); var superagent = require('superagent'); var cheerio = require('cheerio'); var app = express(); app.get('/', function (req, res, next) { superagent.get('http://www.cnblogs.com/rubylouvre/') .end(function (err, sres) { if (err) { return next(err); } // sres.text 里面存储着网页的 html 内容,将它传给 cheerio.load 之后 // 就可以得到一个实现了 jquery 接口的变量,我们习惯性地将它命名为 `$` // 剩下就都是 jquery 的内容了 var $ = cheerio.load(sres.text); var items = []; $("#main > .post h2 > a").each(function (index, element) { var $element = $(element); items.push({ "title": $(this).text(), "url": $(this).attr("href") }); }); res.send(items); }); }); app.listen(4000, function () { console.log('app is listenling at port 4000'); });
是不是非常简单,这个只是最初级的爬虫,一般复杂的需要匹配正则筛选主要的数据进行入库如MongoDB等,不同层级、翻页爬数据这才是一个合格的爬虫。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
最近因为工作中使用node.js来开发工具,所以想到了分享下之前写的简单爬虫!
目的
准备
文档参考
具体编码
是不是非常简单,这个只是最初级的爬虫,一般复杂的需要匹配正则筛选主要的数据进行入库如MongoDB等,不同层级、翻页爬数据这才是一个合格的爬虫。
The text was updated successfully, but these errors were encountered: