var page = require("webpage").create();
page.open("http://yourwebsite", function(status) {
console.log("Status: " + status);
if(status === "success") {
page.evaluate(function(){
document.getElementById("firstName").value = "John";
document.getElementById("lastName").value = "Doe";
});
}
phantom.exit();
});
因此,当使用 phantomjs 对脚本不断更新的数据进行网络抓取时,例如分数或股票价格。如何创建持久打开的浏览器状态来收集这些数据,而无需使用 setInterval/Timeout
调用 page.open("http://yourwebsite", function(status) { }
在循环中?
phantonjs 中是否有一个 api 调用实际上在 headless 状态下像真正的浏览器一样执行?或者我只是重新加载页面并从每次重新加载中读取静态数据?
最佳答案
您不必重新打开页面,最多您必须手动跟踪 setInterval()
调用的状态以确定何时退出:
var page = require("webpage").create();
page.open("http://yourwebsite", function(status) {
console.log("Status: " + status);
if(status === "success") {
var count = 0;
setInterval(function() {
if (count++ === 5) {
//You will need to figure out when to exit!!!!
phantom.exit();
}
page.evaluate(function(){
document.getElementById("firstName").value = "John";
document.getElementById("lastName").value = "Doe";
});
}, 5000/*5 SECONDS*/);
} else {
phantom.exit(1);
}
});
关于javascript - 如何用PhantomJS动态抓取 "changing"数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46657844/