RSS我是从diygod大佬博客里看到的
看概念一开始完全懵逼,但是下了一个RSS阅读器后就大概知道是个什么东西了,老实说,这个十分实用,对于我这种长期坐在电脑前的死宅来说尤其如此。它省了我很多去各个网站浏览的时间–因为它把所有网站集成了。
这个怎么实现呢,首先是通过RSS阅读器解析一个网站的RSS,通过RSS获取这个网站的信息。而如何获取信息的方式,就全部写在RSS里,但是每个网站内容,格式,形式不一样,所以RSS也不一样,就是说,每个RSS都要单独编写。
所以在RSS出来后就火了一阵子,但是问题来了。首先不一定有人做这个网站的RSS,做了也可能找不到,找到了也可能因为页面更新换代用不了,另外RSS 不利于网站方的广告投放、隐私搜集、用户存留等商业行为。这就是RSS难以兴起的原因= =。
RSSHub是diygod发起的一个开源项目,目的是简化和标准RSS的制作流程,让更多的人可以参与到RSS的制作里面来。当然现在不止这么一个项目可以做出RSS地址,但是目前来看应该是相对比较完善的。
一开始是打算加入项目做一个contributor的,发动要素察觉,发现是nodejs(没学过),发现310个参与者,发现项目大小,好的,你的痛苦实力在我之上,我就做个RSS自娱自乐学学nodejs算了(流下了没能力的泪水)
首先我之前说过RSS实质上是写了一个爬虫,爬取了网页数据,所以我这次要写的就是nodejs爬虫。这次爬的就是中南大学官网的中南要闻。
由于nodejs集成了一些其他东西,所以我先写一个纯nodejs版本的爬虫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const http = require("http"); const cheerio = require("cheerio");
const baseurl = 'http://news.csu.edu.cn/info/1002/143733.htm'; http.get(baseurl, function(res){ var html = ''; res.on('data', function(data){ html += data; }); res.on('end', function(){ var $ = cheerio.load(html); $('.otherTme').each(function(i, e){ console.log($(e).text()); }); }); }).on('error', function() { console.log('获取数据出错!'); });
|
这个就简单爬取了一下标题(实际上我打算就爬个标题,爬个链接href就好了)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| const got = require('@/utils/got'); const cheerio = require('cheerio');
module.exports = async (ctx) => { const baseurl = 'http://www.csu.edu.cn/'; const url = 'http://www.csu.edu.cn/yw.htm'; const response = await got({ method: 'get', url: url, }); const $ = cheerio.load(response.data); const list = $('a'); const count = []; for(let i = 0 ; i < Math.min(list.length, 6); i++) { count.push(i) } const out = await Promise.all( count.map(async (i) => { const each = $(list[i]); const storyLink = each.attr('href'); const item = { title: each.attr('title'), link: storyLink, }; const key = item.link; const responseAuthor = await got({ method: 'get', url: key, });
return Promise.resolve(item); }) ); ctx.state.data = { title: '中南要闻', link: 'http://www.csu.edu.cn/yw.htm', item: out, } };
|
大概就是这样吧,本地部署效果是ok的,就是没有上传到github上去部署,实在是太菜了= =。
再开一个坑,爬取校内通知,但是这个的cookie在请求头中,让人头大,目前还没能做出来,那天想起来再填坑吧