我对PhantomJS有一个问题,即渲染图像显然是当前网站的旧版本。这是Google快讯。我需要修改" Deliver to"选项并设置为" RSS",但在PhantomJS眼中,该网站没有此选项。
我收到了修改userAgent
的建议,但我尝试了很多方法,并且在渲染结果上没有任何不同。
page.settings.userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
我的整个剧本。
'use strict'
var page = require('webpage').create(),
isLoaded,
controller,
isSubmitted = false
page.viewportSize = { width: 1920, height: 1080 }
page.settings.userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
page.open('https://www.google.com/alerts', function() {
isLoaded = true
})
page.onLoadStarted = function() {
log('Loading page...')
}
page.onLoadFinished = function(status) {
log('Page has loaded.')
if (status !== 'success') {
log('The page has failed to load.')
exit()
} else {
if (isSubmitted) {
log('Submitted!')
clear()
exit()
} else {
controller = setInterval(function() {
if (isLoaded) {
isLoaded = false
fillUpInputField('input', 'PhantomJS')
} else {
selectRSSOption()
}
}, 1500)
}
}
}
page.onError = function(msg, trace) {
log(msg)
trace.forEach(function(item) {
log(' ', item.file, ':', item.line)
})
}
// -------------------------------------------------------------------------------------------------
// Methods
// -------------------------------------------------------------------------------------------------
function exit() {
phantom.exit()
}
function log(str) {
console.log(str)
}
function render(name) {
page.render(name + '.png')
}
function fillUpInputField(selector, query) {
page.evaluate(function(selector) {
document.querySelector(selector).focus()
}, selector)
page.sendEvent('keypress', query)
render('fill_up_form.png')
log('Rendered fill_up_form.png')
}
function selectRSSOption() {
page.evaluate(function() {
document.querySelector('.show_options').click()
})
render('show_options.png')
log('Rendered show_options.png')
}