node怎么通过cheerio去分析网页?

给点阳光yh
  • 282
var request = require('request');
var cheerio = require('cheerio');


request('https://music.163.com/#/song?id=760058',function(err,result){
    if(err){
        console.log(err);
    }
    var $ = cheerio.load(result.body);
    console.log($)
})

为啥结果是这样的

{ [Function: initialize]
  fn:
   initialize {
     constructor: [Circular],
     _originalRoot:
      { type: 'root',
        name: 'root',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object: null prototype] {},
        'x-attribsNamespace': [Object: null prototype] {},
        'x-attribsPrefix': [Object: null prototype] {},
        children: [Array],
        parent: null,
        prev: null,
        next: null } },
  load: [Function],
  html: [Function],
  xml: [Function],
  text: [Function],
  parseHTML: [Function],
  root: [Function],
  contains: [Function],
  merge: [Function],
  _root:
   { type: 'root',
     name: 'root',
     namespace: 'http://www.w3.org/1999/xhtml',
     attribs: [Object: null prototype] {},
     'x-attribsNamespace': [Object: null prototype] {},
     'x-attribsPrefix': [Object: null prototype] {},
     children: [ [Object], [Object] ],
     parent: null,
     prev: null,
     next: null },
  _options:
   { withDomLvl1: true,
     normalizeWhitespace: false,
     xml: false,
     decodeEntities: true } }

回复
阅读 1.4k
1 个回答

获取到 $ 后直接和 jquery 一样,用选择器去获取dom元素就可以了。

var request = require('request');
var cheerio = require('cheerio');
request('https://music.163.com/#/song?id=760058',function(err,result){
    if(err){
        console.log(err);
    }
    var $ = cheerio.load(result.body);
    console.log($('title').text()); // 打印网易云音乐
})

具体的爬虫实战,可以参考我的爬虫项目,https://github.com/yhlben/cdfang-spider/

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏