nodejs

作者: poppyl | 来源:发表于2016-11-16 09:33 被阅读0次

npm install cheerio'''

chinese:http://cnodejs.org/topic/5203a71844e76d216a727d2e
english:https://github.com/cheeriojs/cheerio


这个是和jQuery很想,可以对html进行处理
下面是一个慕课网网络爬虫的

var http=require('http');
var url='http://www.imooc.com/learn/348'
var cheerio=require('cheerio')

function filterChapters(html){
var $=cheerio.load(html)
var chapters=$('div.chapter')
// [{

// }]
var courseData=[]
 chapters.each(function () {
        var chapter=$(this) // $(this)的用法可以让回调方法省略参数
        // var chapterTitle = chapter.find('strong').text().trim()
        var chapterTitle = chapter.find('strong').contents().filter(function() { return this.nodeType === 3; }).text().trim(); 
        var videos=chapter.find('ul').children()
        var chapterData = { // 定义一个json以接收数据
            chapterTitle : chapterTitle,
            videos:[]
        }

        videos.each(function () {
            var video=$(this).find('a')
            var temp=video.text().trim()
            // var temp=video.contents().filter(function() { return this.nodeType === 3; }).text().trim(); 
            var arr = temp.split('\n') // 多层标签的文本都拼到一起了,要拆开,取用需要的值
            var videoTitle = arr[0].trim() + ' ' +arr[1].trim()
            var id=video.attr('href').split('video/')[1].trim()

            chapterData.videos.push({ // 填写数据 
                title : videoTitle,
                id : id
            })
        })

        courseData.push(chapterData)
    })

    return courseData
}
function printCourseData(courseData) {
    courseData.forEach(function (item) {
        var chapterTitle = item.chapterTitle

        console.log(chapterTitle )

        item.videos.forEach(function (video) {
            console.log('---【'+video.id + '】 ' + video.title.trim() )
        })
    })
}
http.get(url,function(res){
    var html=''
    res.on('data',function(data){
        html+=data
    })
    res.on('end',function(){
        var courseData= filterChapters(html)
        printCourseData(courseData)
    })
}).on('error',function(){
    console.log('get failed')
})

===

相关文章

  • nodejs 到底是什么?

    理解 NodeJs Nodejs 自己使用了Openssl.在Nodejs 0.6之前, Nodejs是动态链接到...

  • centos7.3安装vue-cli

    1、安装vue需要安装nodejs,先去nodejs官网下载nodejs,https://nodejs.org/e...

  • NodeJS-简介&配置

    NodeJS-简介&配置 NodeJS NodeJS 中文网 NodeJS API 一、客户端的JavaScrip...

  • gulp最佳入门@小四

    一、安装nodeJs 说明:gulp是基于nodeJS,理所当然需要安装nodeJS; 安装:打开nodejs官网...

  • 项目构建---全步骤

    nodeJS安装 1.使用bower必须要安装nodeJS,因为bower就是用nodeJS编写的,nodeJS是...

  • 笔记 第六天 nodejs模块

    nodejs模块 nodejs 的文件操作 nodejs的io键盘交互 nodejs的url判断渲染模板 node...

  • nodeJs的下载及安装

    一、下载nodeJs 1. nodeJs的下载路径 (1)nodeJs官网:http://nodejs.cn/(2...

  • nodejs笔记

    nodejs教程 :http://www.runoob.com/nodejs/nodejs-tutorial.ht...

  • Cordova入门配置

    [TOC] Cordova 安装 安装NodeJS下载NodeJS https://nodejs.org/en/...

  • NodeJS 学习资料

    nodejs资源汇总(新手)从零开始nodejs系列文章Nodejs提炼与升华(一) 前言、Nodejs简介Nod...

网友评论

      本文标题:nodejs

      本文链接:https://www.haomeiwen.com/subject/yzalpttx.html