亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

怎么爬取html頁碼的meta信息?

怎么爬取html頁碼的meta信息?

反手de溫柔 2018-01-02 10:06:35
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個贊

已搞好,貼上正確答案留給新人;

有兩種方式,一種直接獲?。?/p>

var?chapters?=?$('meta[name="description"]').attr('content')

這樣可以直接獲取頁面中description的content值,其它信息相同。

第二種:each循環

chapters.each(function(item){
if($(this).attr('name')?==?"description"){
??var?chapter?=?$(this).attr('content')
}
//這樣chapter會取得description的content值,
//注意:一個html里有兩個或多個meta,所以循環需要全部做判斷,不然當循環至description之外的時會提示undefined
})


查看完整回答
1 反對 回復 2018-01-02
  • 1 回答
  • 0 關注
  • 1888 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號