jsdom 使用

它的功能和cheerio 類似。

用來在node上解析html。

但是它還有加載頁面的功能。

jsdom.env(
  "http://www.baidu.com",
  ["http://code.jquery.com/jquery.js"],
  function (err, window) {
   // console.log("there have been", window.$("a").length , "io.js releases!");
    console.log(window.$('title').html())
    console.log(window.$('.sizeBox').html())   
   
  }

指定網址和注入的js地址,回調裏可以得到window

用注入的jquery就可以操作頁面了。

在6.x的node下還可以訪問ssl 頁面。

當然它也只是獲取頁面源碼注入了js。對於ajax的還是無法解決。



當然也可以引用本地的js

var jquery = fs.readFileSync("jquery.js", "utf-8");


var w=jsdom.env({
	url:'http://www.baidu.com',
	//scripts:['http://code.jquery.com/jquery.js'],
	src:[jquery],
	done:function(err,window){
		console.log(window.$('.sizeBox').html())

	},onload:function(window){
		//console.log(window.$('.sizeBox').html())
	}
});


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章