我正在抓取http://www.asx.com.au/asx/markets/optionPrices.do?by=underlyingCode&underlyingCode=XJO
它首先显示一个空白的白色页面,该页面中有一些混淆的JS代码。
该代码自动发送 POST 请求,然后加载实际页面。
我有这段代码来跟踪重定向的页面,但它不起作用。
var page;
var myurl = "http://www.asx.com.au/asx/markets/optionPrices.do?by=underlyingCode&underlyingCode=XJO";
var renderPage = function (url) {
page = require('webpage').create();
page.onNavigationRequested = function (url, type, willNavigate, main) {
if (main && url != myurl) {
myurl = url;
console.log("redirect caught")
// GUILTY CODE
renderPage(url);
}
};
page.open(url, function (status) {
if (status === "success") {
console.log("success")
page.render('yourscreenshot.png');
phantom.exit(0);
} else {
console.log("failed")
phantom.exit(1);
}
});
}
renderPage(myurl);
它只输出
success
redirect caught
看看我的代码,为什么GUILTY CODE
部分没有被执行...为什么renderPage(url)
在重定向捕获后没有被调用?
最佳答案
据我了解,phantomJS 并不能很好地处理重定向。这可能是你的问题。您可能想以不同的方式对此进行测试。或者您可以使用其他浏览器执行这些测试来确认。查看这个 git 问题以了解我的意思 https://github.com/ariya/phantomjs/issues/10389 .
关于javascript - 为什么 PhantomJS 不抓取它重定向到的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43680877/