我正在使用 PhantomJS在 JavaScript DOM 操作发生后获取网页的生成源。这个网页只有一个 <body>
没有别的。
重要:此网页使用浏览器的 localStorage
生成页面。
我想在打开页面之前更改 PhantomJS 中的 LocalStorage。
App.js:
var page = require('webpage').create();
page.open("https://sample.com")
setTimeout(function(){
// Where you want to save it
page.render("screenshoot.png")
// You can access its content using jQuery
var fbcomments = page.evaluate(function(){
return $("body").contents().find(".content")
})
phantom.exit();
}, 1000)
最佳答案
特定域的 localStorage 仅在您打开该域上的页面时可用。你可以
- 在您感兴趣的域上打开一些 URL,
- 根据需要更改
localStorage
, - 在同一域中打开您的目标网址。
这看起来像这样:
page.open("https://sample.com/asdfasdf", function(){
page.evaluate(function(){
localStorage.setItem("something", "whatever");
});
page.open("https://sample.com", function(){
setTimeout(function(){
// Where you want to save it
page.render("screenshoot.png")
// You can access its content using jQuery
var fbcomments = page.evaluate(function(){
return $("body").contents().find(".content")
})
phantom.exit();
},1000)
});
});
也可以在步骤 1 中不打开完整页面。您也可以使用带有某些 URL 的虚拟页面。
page.setContent("", "https://sample.com"); // doesn't actually open any page
page.evaluate(function(){
localStorage.setItem("something", "whatever");
});
page.open("https://sample.com", function(){
setTimeout(function(){
// Where you want to save it
page.render("screenshoot.png")
// You can access its content using jQuery
var fbcomments = page.evaluate(function(){
return $("body").contents().find(".content")
})
phantom.exit();
}, 1000)
});
关于javascript - PhantomJS- 默认使用 LocalStorage 打开页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35267191/