node.js - How to set userAgent property in node-phantom? -
how can set custom useragent property?
tried use
page.set('settings.useragent','mycustomuseragent')
but doesn't seem work.
tried check changes via 'page.get'
page.get('settings')
.then(function(settings){
console.log(settings);
})
.catch(function(err){
console.error(err);
})
but thing see is:
{ 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, gecko) phantomjs/2.1.1 safari/538.1', websecurityenabled: true } here code
'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, 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 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 main page', uri, err); }) .then(function (result) { callback(null, result); }) .finally(function () { return this.page.close(); }) .finally(function () { return this.ph.exit(); }); }
frustrating layers of api abstraction :(
what worked me following api guidelines node-phantom: https://github.com/amir20/phantomjs-node#page-object-api
phantomjs uses page.set(.. whereas phantomjs-node uses page.setting(.. both read , write.
camelcased confusion ;)
this._page.setting('useragent', 'foobar'); return this._page.setting('useragent'); // returns 'foobar' i had same issue now, , question helped me narrow down. thanks!
Comments
Post a Comment