node.js 获取csdn首页热点新闻
2015-08-11 12:08
615 查看
var http = require('http'); var cheerio = require('cheerio'); http.get('http://www.csdn.net/', function(res) { console.log('status code:' + res.statusCode); var page = ''; res.on('data', function(data) { page += data.toString(); }); res.on('end', function() { if (res.statusCode == 200 && !page.empty) { var results = ParseMainNews(page); for (i in results) { console.log(results[i].title + '(' + results[i].href + ')'); } } }); }).on('error', function(err) { console.log(err); }); function ParseMainNews(page) { var results = new Array(); var $ = cheerio.load(page); $('.news_list>ul>li').each(function() { results.push({ title: $('a', $(this)).attr('title'), href: $('a', $(this)).attr('href') }); }); return results; } var seconds = 5; var id = setInterval(function() { console.log(seconds--); if (seconds === 0) { console.log('bye bye ...'); clearInterval(id); } }, 1000);
输出:
status code:200
云安全平台Zscaler B轮融资1亿美元,估值已达10亿美元(http://www.csdn.net/article/2015-08-10/2825428)
社交产品后端架构设计(http://www.csdn.net/article/2015-08-10/2825423)
从Java转向Go——AeroFS的一段奇妙之旅(http://www.csdn.net/article/2015-08-11/2825432-AeroFS)
Rust开发技术���习路线(http://special.csdncms.csdn.net/rust/index.shtml)
MDCC 2015|首批嘉宾名单揭晓 Twitter CTO空降北京(http://www.csdn.net/article/2015-08-11/2825431)
iOS App研发的最后冲刺:内测与部署(http://www.csdn.net/article/2015-08-10/2825427)
为什么你需要近距离接触Rust?(http://www.csdn.net/article/2015-08-07/2825403)
Google产品总监:改良Apple Watch UI的若干设想(http://www.csdn.net/article/2015-08-10/2825421)
5
4
3
2
1
bye bye ...
相关文章推荐
- grunt的使用一(压缩任务)
- Windows环境下的NodeJS+NPM+Bower安装配置
- Nodejs forEach
- nodejs 播放媒体文件
- node 调试debug
- opencontrail编译contrail-web-core时需要安装的外部工具nodejs
- Node.js开发入门—使用jade模板引擎
- Leetcode #117 Populating Next Right Pointers in Each Node II
- Leetcode #116 Populating Next Right Pointers in Each Node
- LeetCode(25)Reverse Nodes in k-Group
- LeetCode(25)Reverse Nodes in k-Group
- LeetCode(24) Swap Nodes in Pairs
- LeetCode(24) Swap Nodes in Pairs
- leetcode 117 —— Populating Next Right Pointers in Each Node II
- leetcode 116 —— Populating Next Right Pointers in Each Node
- NodeJS之Mac初体验
- Count Complete Tree Nodes
- Mac上的Node安装及npm的使用
- leetCode #237 Delete Node in a Linked List
- dfs.datanode.max.xcievers&dfs.datanode.max.transfer.threads介绍