gpt4 book ai didi

javascript - PhantomJS 的 Phantom Node 包等效代码

转载 作者:搜寻专家 更新时间:2023-11-01 00:40:33 24 4
gpt4 key购买 nike

我很难将此代码转换为可在 Node 服务器中使用。所以这段代码被编写为在 PhantomJS 进程中运行(即 $: phantomjs index.js),但我想使用包 require("phantom"); 在 Node 服务器中运行它。但是,我无法让这两个回调正常工作。

page.onLoadFinished = function(status){
console.log("Load Finished");
};
page.onUrlChanged = function(){
console.log("URL Changed");
};

这是我试图对整个情况进行 Node 化的可悲尝试。

  phantom.create(['--ignore-ssl-errors=yes','--load-images=no']).then(function(ph) {
console.log("here");
ph.createPage().then(function(page) {
page.property('onResourceRequested', function(requestData, networkRequest) {
console.log(requestData.url);
});
page.open('https://example.com/login').then(function(status) {
console.log(status);
if (status !== 'success') { console.log("failed connection")} else {

page.evaluate(function() {
document.getElementById('email').value = "stuff";
document.getElementById('password').value = "things";
setTimeout(document.getElementsByTagName('button')[0].click(),5000);
console.log("login attempt");
setTimeout(document.URL, 2000);
});
page.onLoadFinished = function(status){
console.log("Load Finished");
};
page.onUrlChanged = function(){
console.log("url changed");
};

}
});
});
});

代码也能正常工作并获取页面并单击按钮,但问题是在幻影登录后,我需要来自下一页的数据,我将使用 onUrlChanged 和 onLoadFinished 来完成。

最佳答案

page.onLoadFinished 和 page.onUrlChanged 是在页面打开之后执行的回调函数,因此在打开 url 之前分配它们是有意义的。

订阅网页的console.log和错误信息也是一个有用的习惯。

  var phantom = require('phantom');
phantom.create(['--ignore-ssl-errors=yes','--load-images=no']).then(function(ph) {
console.log("here");
ph.createPage().then(function(page) {

page.property('onError', function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
});

page.property('onConsoleMessage', function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
});

page.property('onResourceRequested', function(requestData, networkRequest) {
console.log(requestData.url);
});

page.property('onLoadFinished', function(status) {
console.log("Load Finished with status " + status);
});

page.property('onUrlChanged', function(targetUrl) {
console.log("URL changed to: " + targetUrl);
});

page.open('https://example.com/login').then(function(status) {

if (status !== 'success') { console.log("failed connection")} else {

page.evaluate(function() {
document.getElementById('email').value = "email";
document.getElementById('password').value = "password";

setTimeout(function(){
console.log("login attempt");
document.getElementsByTagName('button')[0].click();
}, 5000);
});

});

}
});
});
});

关于javascript - PhantomJS 的 Phantom Node 包等效代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36117239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com