为什么PhantomJS会显示旧版本的Google快讯?

时间:2017-01-30 08:09:26

标签: javascript phantomjs

我已经下载了PhantomJS,因为我需要创建Google Alert RSS Feed,但他们不提供API。

我注意到所访问的URL的渲染图像是旧的,因此,没有找到页面的元素来执行所需的操作,例如更改" Deliver to"成为" RSS" ...

这是什么?错误?为什么网站与通过浏览器访问不同?

我的剧本:

var page = require('webpage').create(),
    isLoaded,
    controller,
    isSubmitted = false

page.open('https://www.google.com/alerts', function() {
  page.viewportSize = { width: 1920, height: 1080 }
  page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36'

  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()
        }
      }, 7000)
    }
  }
}

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 :(得分:0)

您需要欺骗浏览器使用者,因为默认情况下,Google会将PhantomJS视为较旧的移动浏览器(也是因为默认视口较小)。尝试使用Firefox,Chrome或Edge的现代用户。

var page = require('webpage').create();

page.viewportSize = { width: 1280, height: 800 };
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36';