eCo*_*Evo 6 javascript phantomjs casperjs
在执行CasperJS脚本的过程中,我需要从另一个站点获取并解析JSON数据,以便我可以使用该数据填写我正在积极处理的站点上的表单.
我怎样才能做到这一点?
你可以使用__utils__.sendAJAX():
var casper = require('casper').create();
var wsurl = 'https://raw.github.com/n1k0/casperjs/master/package.json';
var word;
casper.start('http://google.com/', function() {
word = this.evaluate(function(wsurl) {
try {
return JSON.parse(__utils__.sendAJAX(wsurl, 'GET', null, false)).name;
} catch (e) {
}
}, {wsurl: wsurl});
});
casper.then(function() {
if (!word) {
this.die('unable to retrieve word');
}
this.echo('searching for ' + word);
this.fill('form[action="/search"]', {q: word}, true);
});
casper.run(function() {
this.echo(this.getCurrentUrl());
this.exit();
});
Run Code Online (Sandbox Code Playgroud)
样本执行(不要忘记通过--web-security=no):
$ casperjs test.js --web-security=no
searching for casperjs
http://www.google.fr/search?hl=fr&source=hp&q=casperjs&gbv=2&oq=&gs_l=
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.