快捷导航

怎么爬取html页码的meta信息?

var http = require('http');var cheerio = require('cheerio')var url = 'http://www.dodzz.com/links.html'function filterChapters(html){var $ = cheerio.load(html)var chapters = $('meta')chapters.each(function(item){var chapter = $(this).find('keywords').attr('content')//这里运行后 没有任何提示(我想问下怎么爬取html页面中meta属性内容)console.log(chapter)})http.get(url,function(res){var html = ''res.on('data',function(data){html += data})res.on('end',function(){filterChapters(html)})}).on('error',function(){console.log('error')})

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员1

已搞好,贴上正确答案留给新人;
有两种方式,一种直接获取:
varchapters=$('meta[name="description"]').attr('content')这样可以直接获取页面中description的content值,其它信息相同。
第二种:each循环
chapters.each(function(item){if($(this).attr('name')=="description"){varchapter=$(this).attr('content')}//这样chapter会取得description的content值,//注意:一个html里有两个或多个meta,所以循环需要全部做判断,不然当循环至description之外的时会提示undefined})
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP