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 回答

反手de温柔
TA贡献4条经验 获得超1个赞
已搞好,贴上正确答案留给新人;
有两种方式,一种直接获取:
1 | var chapters = $( 'meta[name="description"]' ).attr( 'content' ) |
这样可以直接获取页面中description的content值,其它信息相同。
第二种:each循环
1 2 3 4 5 6 7 | chapters.each( function (item){ if ($( this ).attr( 'name' ) == "description" ){ var chapter = $( this ).attr( 'content' ) } //这样chapter会取得description的content值, //注意:一个html里有两个或多个meta,所以循环需要全部做判断,不然当循环至description之外的时会提示undefined }) |
添加回答
举报
0/150
提交
取消