【问题标题】:How to set userAgent property in node-phantom?如何在 node-phantom 中设置 userAgent 属性?
【发布时间】:2017-03-25 00:52:44
【问题描述】:

如何设置自定义 userAgent 属性?
我尝试使用

page.set('settings.userAgent','myCustomUserAgent')

但它似乎不起作用。
我还尝试通过 'page.get'

检查更改

page.get('settings')
.then(function(settings){
console.log(settings);
})
.catch(function(err){
console.error(err);
})

但我唯一看到的是:


    {
      XSSAuditingEnabled: false,
      javascriptCanCloseWindows: true,
      javascriptCanOpenWindows: true,
      javascriptEnabled: true,
      loadImages: true,
      localToRemoteUrlAccessEnabled: false,
      userAgent: 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1',
      webSecurityEnabled: true 
    }

这是我的代码

'use strict';
const async = require('async');
const phantom = require('node-phantom-async');
process.setMaxListeners(Infinity);

const uri = 'http://www.my.uri';

function parseCatalogLinks(callback) {
    phantom.create({
        phantomPath: require('phantomjs').path,
        ignoreSSLErrors: true
    })
        .bind({})
        .then(function (ph) {
            this.ph = ph;
            return this.ph.createPage();
        })
        .then(function (page) {
            this.page = page;
            this.page.set('settings.userAgent', 'Mozilla/5.0 (Windows NT 6.3; WOW64)' +
                ' AppleWebKit/537.36 (KHTML, like Gecko)' +
                ' Chrome/54.0.2840.87 Safari/537.36');
            return this.page;
        })
        .then(function (page) {
            this.page.get('settings')
                .then(function (settings) {
                    console.log('settings', settings)
                })
                .catch(function (err) {
                    console.error(err);
                });
            return this.page.open(uri);
        })
        .catch(function (err) {
            console.error('Error while opening main page', err);
        })
        .then(function (status) {
            console.info('Main site ' + uri + ' opened with status', status);
        })
        .then(function () {
            return this.page.evaluate(function () {
                var $catalogLinksList = $('.header .category .category__item');
                var catalogItems = [];

                $catalogLinksList.each(function () {
                    catalogItems.push({
                        sectionLink: $('a.category__link', this).attr('href'),
                        sectionTitle: $('.category__title', this).text()
                    })
                });
                return catalogItems;
            })
        })
        .catch(function (err) {
            console.error('Error while evaluating the main page', uri, err);
        })
        .then(function (result) {
            callback(null, result);
        })
        .finally(function () {
            return this.page.close();
        })
        .finally(function () {
            return this.ph.exit();
        });
}

【问题讨论】:

    标签: node.js phantomjs user-agent


    【解决方案1】:

    令人沮丧的 API 抽象层 :(

    对我有用的是遵循 node-phantom 的 API 指南: https://github.com/amir20/phantomjs-node#page-object-api

    PhantomJS 使用 page.set(.. 而 phantomjs-node 使用 page.setting(.. 来读写。

    camelCased 造成额外的混乱;)

      this._page.setting('userAgent', 'foobar');     
    
      return this._page.setting('userAgent'); // returns 'foobar'
    

    我刚才遇到了同样的问题,您的问题帮助我缩小了范围。谢谢!

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 2021-08-05
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多