我是PhantomJS的新手,試圖捕獲Trade Me的主頁。這裏是我到目前爲止的代碼:在所有內容異步加載後捕獲頁面
var page = require('webpage').create();
page.open('http://trademe.co.nz', function() {
// Checks for bottom div and scrolls down from time to time
window.setInterval(function() {
// Checks if there is a div with class=".has-more-items"
// (not sure if this is the best way of doing it)
// var count = page.content.match(/class=".site-footer"/g);
var footer_visible = page.evaluate(function() {
return $('.site-footer').is(':visible');
});
if(!footer_visible) { // Didn't find
console.log('Scrolling');
page.evaluate(function() {
// Scrolls to the bottom of page
window.document.body.scrollTop = document.body.scrollHeight;
});
}
else { // Found
console.log('Found');
// Do what you want
window.setTimeout(function() {
console.log('Capturing');
page.render('phantom-capture.png', {format: 'png'});
phantom.exit();
}, 10000);
}
}, 1000); // Number of milliseconds to wait between scrolls
});
有幾件事情是難不倒我:
- 永遠不會被打印的字
Scrolling
。 - 它最終得到
Found
,並且該單詞被打印10次。我認爲這是因爲它包含在setInterval
塊中,間隔爲1秒,並且由setTimeout
引起的等待時間爲10秒? - 頁面最終呈現給PNG文件,但這些異步加載的面板的內容仍爲空,並顯示
Loading...
消息。
我是新來的這一切,我的Javascript知識是很生疏。
檢查'carousel-loading-card'項目的好主意! –