PhantomJS-默认使用LocalStorage打开页面
PhantomJS- open page with LocalStorage by default
我正在使用PhantomJS在JavaScript DOM操作发生后获取网页的生成源代码。这个网页只有一个<body>
,没有别的。
重要提示
:此网页使用浏览器localStorage
来生成网页。
我想在打开页面之前更改 PhantomJS 中的本地存储。
应用.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)
});
我用朋友的帮助解决了我的问题。我的解决方案是:
注意:我为完成加载页面设置了超时 10000(10 秒)。
var page = require('webpage').create();
page.open("https://sample.com", function(){
page.evaluate(function(){
var i = 0,
oJson = jsonData,
sKey;
localStorage.clear();
for (; sKey = Object.keys(oJson)[i]; i++) {
localStorage.setItem(sKey,oJson[sKey])
}
});
page.open("https://sample.com", function(){
setTimeout(function(){
page.render("screenshoot.png")
// Where you want to save it
console.log(page.content); //page source
// You can access its content using jQuery
var fbcomments = page.evaluate(function(){
return $("body").contents().find(".content")
})
phantom.exit();
},10000)
});
});
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 用嵌套函数和默认函数定义函数
- 如何将localStorage值设置为false
- 如何使用铁流星与流星的默认路线
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 如果localstorage为空,则显示欢迎消息
- CKeditor:更改对话框中的默认选择选项
- 在不阻止默认行为的情况下检测IE10中的缩放
- 如何从外部页面激活非默认引导选项卡
- 防止默认锚点行为AngularJS
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- jpm的默认Firefox路径没有'不起作用
- jquerydatetimepicker日期和时间应在框默认值中,而无需单击框
- 如果值为空,如何设置输入的默认值?jQuery
- 骨干模型默认值-todos.js示例中不必要的代码
- jquery日期选择器年份范围默认值
- jQuery:当屏幕大小改变时,如何更改默认图像和悬停图像
- PhantomJS-默认使用LocalStorage打开页面
- 默认情况下,localstorage也是用户特定的
- 使用默认选中复选框的LocalStorage