gpt4 book ai didi

javascript - 使用 phantomjs 和 Jquery 登录网页

转载 作者:数据小太阳 更新时间:2023-10-29 05:58:47 25 4
gpt4 key购买 nike

一般来说,我是 phantomjs、Java 脚本和 WebScraping 的新手。我想做的是基本的 http 身份验证,然后访问另一个 URL 以获取一些信息。这是我到目前为止所拥有的。请告诉我我做错了什么。

var page = require('webpage').create();
var system = require('system');

page.onConsoleMessage = function(msg) {
console.log(msg);
};

page.onAlert = function(msg) {
console.log('alert!!>' + msg);
};

page.settings.userName = "foo";
page.settings.password = "bar";

page.open("http://localhost/login", function(status) {
console.log(status);
var retval = page.evaluate(function() {
return "test";
});
console.log(retval);

page.open("http://localhost/ticket/" + system.args[1], function(status) {
if ( status === "success" ) {
page.injectJs("jquery.min.js");
var k = page.evaluate(function () {
var a = $("div.description > h3 + p");

if (a.length == 2) {
console.log(a.slice(-1).text())
}
else {
console.log(a.slice(-2).text())
}
//return document.getElementById('addfiles');
});

}
});
phantom.exit();
});

我正在向该文件传递一个参数:一个附加到第二个 URL 的票号。

最佳答案

我会推荐 CasperJS对此高度评价。

CasperJS is an open source navigation scripting & testing utility written in Javascript and based on PhantomJS — the scriptable headless WebKit engine. It eases the process of defining a full navigation scenario and provides useful high-level functions, methods & syntactic sugar for doing common tasks such as:

  • defining & ordering browsing navigation steps
  • filling & submitting forms
  • clicking & following links
  • capturing screenshots of a page (or part of it)
  • testing remote DOM
  • logging events
  • downloading resources, including binary ones
  • writing functional test suites, saving results as JUnit XML
  • scraping Web contents

(来自 CasperJS 网站)

我最近花了一天时间尝试让 PhantomJS 自己完成一些事情,比如填写登录表单并导航到下一页。

CasperJS 也有一个很好的为表单构建的 API:

http://docs.casperjs.org/en/latest/modules/casper.html#fill

var casper = require('casper').create();

casper.start('http://some.tld/contact.form', function() {
this.fill('form#contact-form', {
'subject': 'I am watching you',
'content': 'So be careful.',
'civility': 'Mr',
'name': 'Chuck Norris',
'email': 'chuck@norris.com',
'cc': true,
'attachment': '/Users/chuck/roundhousekick.doc'
}, true);
});

casper.then(function() {
this.evaluateOrDie(function() {
return /message sent/.test(document.body.innerText);
}, 'sending message failed');
});

casper.run(function() {
this.echo('message sent').exit();
});

关于javascript - 使用 phantomjs 和 Jquery 登录网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11107331/

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