2016-11-10 11 views
3

カスタムuserAgentプロパティを設定するにはどうすればよいですか?
私はノードファントムでuserAgentプロパティを設定する方法は?

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

を使用しようとしました。しかし、動作するようには思えません。
Iはまた、

page.get( '設定')
.then(関数(設定)'page.get' を介して変更を確認しようとした{
にconsole.log(設定);}

.catch(関数(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 
    }
phantomjs-一方page.set(..を使用しています https://github.com/amir20/phantomjs-node#page-object-api

PhantomJS:210

ここでは、私のコードは、APIの抽象化の

'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(); 
     }); 
} 

答えて

0

イライラ層:(

何私のために働いたことは、ノードファントムからAPIのガイドラインに従っているありますノードは両方ともpage.setting(..を使用して読み取りと書き込みを行います。余計な混乱のため

camelCased;)

this._page.setting('userAgent', 'foobar');  

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

私はちょうど今、同じ問題があったが、あなたの質問は私がこれを絞り込む助けました。ありがとう!

関連する問題