为nodejs模块PhantomJS配置代理
PhantomJS模块配置代理方案一:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
phantom.create(["--proxy=201.172.242.184:15124", "--proxy-type=socks5"]) .then((instance) => { phInstance = instance; return instance.createPage(); }) .then((page) => { sitepage = page; return page.open('http://newsdaily.online'); }) .then((status) => { console.log(status); return sitepage.property('title'); }) .then((content) => { console.log(content); sitepage.close(); phInstance.exit(); }) .catch((error) => { console.log(error); phInstance.exit(); }); |
PhantomJS模块配置代理方案二:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
const phantomInstance = await require('phantom').create(); const page = await phantomInstance.createPage(); // get current settings: var pageSettings = await page.property('settings'); /* { XSSAuditingEnabled: false, javascriptCanCloseWindows: true, javascriptCanOpenWindows: true, javascriptEnabled: true, loadImages: true, localToRemoteUrlAccessEnabled: false, userAgent: 'Mozilla/5.0 (Unknown; Linux x86_64) ... PhantomJS/2.1.1 Safari/538.1', webSecurityEnabled: true } */ pageSettings.proxy = 'https://78.40.87.18:808'; // update settings (return value is undefined): await page.property('settings', pageSettings); const status = await page.open('https://2ip.ru/'); // show IP: var ip = await page.evaluate(function () { var el = document.getElementById('d_clip_button'); return !el ? '?' : el.textContent; }); console.log('IP:', ip); |
方案三:
代理设置是在进程创建期间设置的,而不是在打开页面期间设置的。尽管PhantomJS包含一个未文档化的phantom.setProxy()函数,该函数允许您在脚本中间更改代理设置,phantom模块似乎也支持它。
0 1 2 3 4 5 6 7 8 9 10 11 |
var phantom = require('phantom'); phantom.create({ parameters: { proxy: '98.239.198.83:21320' } }, function(browser){ browser.createPage(function(page){ page.open('http://example.com/req.php', function() { ... }); }); }); |
参考:https://stackoverflow.com/questions/28571601/how-do-i-set-proxy-in-phantomjs