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())
	}
});


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