使用PhantomJS更改userAgent无法解决旧网站问题(Changing the userAgent with PhantomJS is not solving the old website

编程入门 行业动态 更新时间:2024-10-26 07:39:33
使用PhantomJS更改userAgent无法解决旧网站问题(Changing the userAgent with PhantomJS is not solving the old website issue)

我有一个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.js

The 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:

enter image description here

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

更多推荐

本文发布于:2023-04-27 12:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网站   userAgent   PhantomJS   Changing   issue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!