【发布时间】:2017-04-07 09:58:14
【问题描述】:
我最近进入了 PhantomJS 目前正在使用 PhantomJS 作为我的第一份开发人员工作。
我的任务是从通过 LAN 电缆连接到 PC 的旧 Cisco Catalyst 2960 x 交换机上抓取网络交换机信息(主机名、产品 ID、IP 地址、MAC 地址等)。
我的 http 身份验证在 phantomJS 无头浏览器上运行良好,可以打开第一个切换页面,但它会导致如下图所示的启动页面。cisco switch startup report
此启动页面仅在第一次登录/访问开关后出现,用户必须单击具有如下所示表单按钮输入属性的继续按钮。 (顺便用AJAX写的)
<form METHOD="GET">
<input type="button" name="button1" value="Continue"
onclick="setcookiesandLoadCiscoDeviceManager()"></form>
通常在 Chrome 浏览器上,我们单击它并继续。随后将我们带到感兴趣的主页,即包含交换机信息的 Cisco 设备管理器页面。(不允许发布图片,但可在 phantomjs 小组讨论页面上找到)
我的问题是,用 phantomJS 无头浏览器绕过启动报告最好的方法是什么?要么……
- 在表单提交上模拟按钮按下方法 GET 触发链接转到下一页(想到 $.ajax())或...
- 通过 .js 文件调用函数 setcookiesandLoadCiscoDeviceManager()(更多关于后者)。这更像是一种黑客攻击方法。
交换机网页架构概述here
当第一次请求 URL 10.44.39.252 时,会调用 3 帧 src。我通过 phantomjs 回调知道这一点
page.onNavigationRequested
- Frmwrkresource.htm
- topbannernofpv.shtml
- setup_report.htm
输入“button1”存在于 setup_report.htm 框架内。当“button1”被按下时
setscookiesandLoadsCiscoDeviceManager();
被称为
此函数调用仅存在于 preflight.js 中,所有被称为在 startup_report 和 Cisco 设备管理器 (10.44.39.252/xhome.htm) 之间转换的 javascript 资源中。我认为浏览器 cookie 是这个问题的主要部分。
附上我的源代码。它处于不同的完成阶段
var page = require('webpage').create();
var fs = require('fs');
console.log("\n:Welcome to my Crawler Scrapper:");
var url = 'http://10.44.39.252/';
page.settings.userName='star';
page.settings.password='----------';
page.customHeaders={'Authorization': 'Basic '+btoa('star:xzsawq4321')};
page.settings.userAgent = 'PMG Web Crawler Bot/1.0';
page.onNavigationRequested = function(url,type,willNavigate, main){
console.log("\n----------------------------------------------");
console.log("Navigation Request Information:\n")
console.log('Trying to navigate to: ' + url);//where are you going?
console.log('Caused by: ' + type); //request type
console.log('Will it actually navigate: ' + willNavigate);
console.log('Sent from the page\'s main frame: ' + main);
console.log("----------------------------------------------\n");
};
page.onResourceError = function(resourceError){
console.log("\nHold Up, We have Errors!")
console.log("Resource Error Information: \n")
console.log('Resoruce ErrorID:' + resourceError.id + '\nURL:' +
resourceError.url);
console.log('Resource Error Code: ' + resourceError.errorCode +
'\nDescription: ' + resourceError.errorString);
};
page.onConsoleMessage = function(msg) {
console.log("The Browser Replied:" + msg);
};
//////////////////////////////////////////////////////////////////
page.onLoadStarted = function(){
console.log("Loadng Page...")
};
page.onLoadFinished = function(){
console.log("Loading finished:\n");
};
//////////////////////////////////////////////////////////////////
page.viewportSize = {
width: 1920,
height: 1200
};
var sel = 'button1'; //DOM manipulate, selector
var type = 'click', //action
//webpage.open
page.open(url,function(status){
if(status === "success"){
page.includeJs(
"http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js",function()
{//jquery syntax has been successfully included
setTimeout(function(){
var t = page.evaluate(function(sel) {
var a = $('title').text();
return a;
},0,sel);
console.log("Title: " + t + "\n\n");
phantom.addCookie({
Cisco_DeviceManager : 'value', /* required property */
SSLPreference : 2, /* required property */
gettingstarted : 1
});
page.open('http://10.44.39.252/xhome.htm', function (status) {
$(document).ready(function(){
console.log("Your Document is Ready:"+ document.title +"\n");
/*ajax assynchronous http request
$.ajax({
async: false,//blocks the ajax call, SYNCHRONOUS ajax Request
url: 'http://10.44.39.252/setup_report.htm?button1=Continue',
type: 'GET',
data: {button1: 'Continue'},
success: function (out) {
console.log("REQUEST SENT!\n\n");
console.log(typeof(out));
$('button1').trigger(sel);
console.log($('.homecontent').text);
//$("button1").click(function(){
// $("input").trigger("select");
//});
},
error: function(){
console.log("Nein!");
}
});
*/
});
});
},3000);
setTimeout(function() {
page.render("phantomspecs1.jpg");
console.log("\nNow GTFO!")
phantom.exit();
},20000);
console.log("Wait for the Async...");//prints first!
},0);//closes includejs which doesnt operate in the next open...
}else{
console.log("Connect fail");
phantom.exit();
}
});
我需要 phantomJS 绕过启动页面并转到 CiscoDeviceManager,我可以在其中呈现交换机信息。但我对 JavaScript、JQuery 和 AJAX 的知识仍然缺乏(不是原生程序员,但大学毕业后我找到了一份编码工作,但我确实有一些基本概念)
如果你们中的任何人可以帮助我为下一步指明正确的方向,我可以完成任务并对其进行记录。毫无疑问,这对 Phantom 社区很有价值。(我很自豪能成为其中的一员)
真诚地, 阿菲克·阿卜杜勒·哈米德, 马来西亚赛城
【问题讨论】:
标签: javascript jquery ajax phantomjs