我有一个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') }I have an issue with PhantomJS that is rendering images that is apparently an older version of the current website. It's Google Alerts. I need to modify the "Deliver to" option and set to "RSS", but in PhantomJS's eyes, the website doesn't have this option.
I was given the suggestion to modify the userAgent, but I've tried many ways and there's no different on the rendered result.
page.settings.userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'My whole script.
'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') }最满意答案
这种不一致背后的原因很简单:在用户登录Google帐户之前,Google快讯没有“提供”选项。
尝试在同一个Chrome中以隐身模式打开该页面:
因此,为了让它在PhantomJS中运行,您需要先登录Google。 使用--cookies-file.txt CLI参数在脚本运行之间保留cookie,这样您每个月只需登录一次。
/path/to/phantomjs --cookies-file=cookies.txt /script/to/run.jsThe reason behind this inconsistency is quite simple: Google Alerts does not have a "deliver" option until a user is signed in to a Google account.
Try opening the page in an incognito mode in the same Chrome:
So, in order to get it working in PhantomJS you need to log in to Google first. Use --cookies-file.txt CLI argument to persist cookies between script runs so that you need to log in only once a month.
/path/to/phantomjs --cookies-file=cookies.txt /script/to/run.js更多推荐
发布评论