Flu*_*ffy 5 node.js web-scraping nightmare
我试图了解如何使用"if-then"逻辑制作一个nightmare.js脚本.例如
var Nightmare = require('nightmare');
var nightmare = Nightmare({
show: true,
paths: {
userData: '/dev/null'
}
});
nightmare
.goto('http://www.example.com/')
.wait('h1')
.evaluate(function() {
return document.querySelector('title').innerText;
})
// here: go to url1 if title == '123' otherwise to url2
.end()
.then(function() {
console.log('then', arguments);
}).catch(function() {
console.log('end', arguments);
});
Run Code Online (Sandbox Code Playgroud)
如何根据评估结果将此脚本转到其他URL?
Ros*_*oss 10
由于梦魇then能够,你可以.then()像往常一样将它从一个连锁中归还给它.
var Nightmare = require('nightmare');
var nightmare = Nightmare({
show: true,
paths: {
userData: '/dev/null'
}
});
nightmare
.goto('http://www.example.com/')
.wait('h1')
.evaluate(function() {
return document.querySelector('title')
.innerText;
})
.then(function(title) {
if (title == 'someTitle') {
return nightmare.goto('http://www.yahoo.com');
} else {
return nightmare.goto('http://w3c.org');
}
})
.then(function() {
//since nightmare is `then`able, this `.then()` will
//execute the call chain described and returned in
//the previous `.then()`
return nightmare
//... other actions...
.end();
})
.then(function() {
console.log('done');
})
.catch(function() {
console.log('caught', arguments);
});
Run Code Online (Sandbox Code Playgroud)
如果您想要一个更具同步性的逻辑,您可能需要考虑使用带有vo或co的生成器.例如,上面改写为:vo
var Nightmare = require('nightmare');
var vo = require('vo');
vo(function * () {
var nightmare = Nightmare({
show: true,
paths: {
userData: '/dev/null'
}
});
var title = yield nightmare
.goto('http://www.example.com/')
.wait('h1')
.evaluate(function() {
return document.querySelector('title')
.innerText;
});
if (title == 'someTitle') {
yield nightmare.goto('http://www.yahoo.com');
} else {
yield nightmare.goto('http://w3c.org');
}
//... other actions...
yield nightmare.end();
})(function(err) {
if (err) {
console.log('caught', err);
} else {
console.log('done');
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3031 次 |
| 最近记录: |