使用PhantomJS更改userAgent无法解决旧网站问题

时间:2017-01-31 07:42:17

标签: javascript phantomjs

我对PhantomJS有一个问题,即渲染图像显然是当前网站的旧版本。这是Google快讯。我需要修改" Deliver to"选项并设置为" RSS",但在PhantomJS眼中,该网站没有此选项。

Image of Google Alerts rendered by 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')
}

1 个答案:

答案 0 :(得分:1)

这种不一致背后的原因非常简单:Google快讯没有"交付"选项,直到用户登录Google帐户。

尝试在同一个Chrome中以隐身模式打开页面:

enter image description here

因此,为了让它在PhantomJS中运行,您需要先登录Google。使用--cookies-file.txt CLI参数在脚本运行之间保留cookie,这样您只需每月登录一次。

/path/to/phantomjs --cookies-file=cookies.txt /script/to/run.js