2015-09-06 25 views
1

我的代碼...Coudn't中啓用JavaScript PhantomJS

var webPage = require('webpage'); 
var page = webPage.create(); 
page.open("http://www.mangaumaru.com/archives/503925", function(status) { 
    console.log(page.content); 
    phantom.exit(); 
}); 

和 「C:\> phantomjs main.js」 的結果是這樣的:

<html><head><title>You are being redirected...</title> 
<noscript>Javascript is required. Please enable javascript 
before you are allowed to see this page.</noscript> 

我怎樣才能解決這個問題問題?

回答

1

PhantomJS默認啓用JavaScript。無論JavaScript是否啓用,<noscript>元素始終是源代碼的一部分。當JavaScript被禁用時,元素不會被渲染。打印源文件不會告訴你很多。您需要渲染截圖,例如page.render("screenshot.png")以查看noscript元素是否可見。

當這樣的JavaScript重定向完成後,通常會在一段時間後觸發。您需要確定是什麼時候爲您的網頁,但(直到下一個頁面加載),您將解決例如或者通過等待一點點:

page.open("http://www.mangaumaru.com/archives/503925", function(status) { 
    setTimeout(function(){ 
     console.log(page.content); 
     phantom.exit(); 
    }, 10000); 
}); 

,或者你可以做註冊頁面加載事件以便您看到第二頁已加載:

page.open("http://www.mangaumaru.com/archives/503925", function(status) { 
    page.onLoadFinished = function(status){ 
     console.log(page.content); // actual page 
     phantom.exit(); 
    }; 
});