我正试图使用噩梦抓取网页,但希望等待#someelem
出现,前提是它确实存在。否则,我希望噩梦继续前进。使用.wait()
怎么能做到这一点?
我不能使用.wait(ms)
。使用.wait(selector)
意味着噩梦将一直等待,直到元素出现,但如果页面永远不会有这个元素,噩梦将永远等待。
最后一个选项是使用.wait(fn)
。我试过这种
.wait(function(cheerio) {
var $ = cheerio.load(document.body.outerHTML);
var attempt = 0;
function doEval() {
if ( $('#elem').length > 0 ) {
return true;
}
else {
attempt++;
if ( attempt < 10 ) {
setTimeout(doEval,2000); //This seems iffy.
}
else {
return true;
}
}
}
return doEval();
},cheerio)
因此,等待并再次尝试(达到阈值),如果找不到元素,则继续前进。setTimeout附近的代码似乎是错误的,因为.wait
是在浏览器范围内完成的。
提前感谢!
我认为传递现有的cheerio
库不会很好。参数被序列化(或多或少)以传递给子Electron进程,因此传递整个库可能不起作用。
在上侧,.wait(fn)
的fn
部分在页面上下文中执行,这意味着您可以完全访问document
及其方法(例如querySelector
)。如果页面的jQuery上下文存在,您也可以访问它,如果不存在,您甚至可以使用.inject()
注入它。
抛开这一点不谈,就.wait()
(和.evaluate()
而言)所期望的同步方法而言,你是对的,至少在类似promise的东西可以直接在.evaluate()
中使用之前是这样。
在这之前,您可以使用.action()
来模拟您想要的行为:
var Nightmare = require('nightmare');
Nightmare.action('deferredWait', function(done) {
var attempt = 0;
var self = this;
function doEval() {
self.evaluate_now(function(selector) {
return (document.querySelector(selector) !== null);
}, function(result) {
if (result) {
done(null, true);
} else {
attempt++;
if (attempt < 10) {
setTimeout(doEval, 2000); //This seems iffy.
} else {
done(null, false);
}
}
}, '#elem');
};
doEval();
return this;
});
var nightmare = Nightmare();
nightmare.goto('http://example.com')
.deferredWait()
.then(function(result) {
console.log(result);
});
- 正如在噩梦的文献中所提到的
.wait(选择器)等待元素选择器出现,例如等待("#支付按钮")
在这种情况下,等待等待只工作到元素第一次变为可见,如果它不可见,那么它将工作到30s 的默认超时
- 使用函数等待
.wait(function () { return (document.querySelector(selector) === null); })
其中,选择器是基于其在DOM中的存在而等待的元素。
在这里,我创建了一个函数来获取不同条件下的html源,我正在爬取TimeWarnerCable页面以获取有关电视、互联网和捆绑包计划的信息,所以我的函数会获取一些参数,并在不同的调用中对每个参数做出反应。您可以使用.exists()检查选择器,然后继续使用梦魇
function getSource(url,serviceQuantity,zip){
var defer=Q.defer();
var Nightmare = require('nightmare');
var nightmare = Nightmare({openDevTools:browserDev ,show: browserVisible,'webPreferences':{partition: 'nopersist'}});
nightmare
.goto(url)
.cookies.clear()
.wait(2000)
.exists('div.messagebox-wrapper.twc-container[style="display: block;"]')
.then(function(noZipSet){
if (noZipSet){
debug('No zipcode settled down');
nightmare
.insert('fieldset > div > input[placeholder="Enter Your ZIP Code"]',zip)
.type('fieldset > div > input[placeholder="Enter Your ZIP Code"]', 'u000d');//I do "Enter" because nightmare can't find the submit button
}else{
debug('Zipcode settled down');
nightmare
.click('div.section.newHeaderIcons > div > ul > li:nth-child(4) > div > a')
.wait(2000)
.insert('form.geoLoc > fieldset > div > input[placeholder="Update Your ZIP Code"]',zip)
.type('form.geoLoc > fieldset > div > input[placeholder="Update Your ZIP Code"]', 'u000d');//I do "Enter" because nightmare can't find the submit button
}
nightmare
.wait(8500)
.exists('div[style="display: block;"] > div > div > div > div > div > div > div.parsys.oof-error-content > div > div > div > div > div > div > p[style="color: #333333;"]')
.then(function(zipNotAvailable){
if (zipNotAvailable){
debug('Service not available in '+zip+' for '+serviceQuantity+' services');
nightmare
.end()
.then(function(){
defer.resolve('');
});
}else{
debug('Service available on the zipcode');
switch (serviceQuantity) {
case 1:
nightmare
.evaluate(function(){
return document.querySelector('html').innerHTML;
})
.end()
.then(function (result) {
defer.resolve(result);
})
.catch(function (error) {
debug('ERROR >> Search failed:', error);
});
break;
case 2:
nightmare
.click('#tv-filter')
.wait(500)
.click('#internet-filter')
.wait(500)
.evaluate(function(){
return document.querySelector('html').innerHTML;
})
.end()
.then(function (result) {
defer.resolve(result);
})
.catch(function (error) {
debug('ERROR >> Search failed:', error);
});
break;
case 3:
nightmare
.click('#tv-filter')
.wait(500)
.click('#internet-filter')
.wait(500)
.click('#phone-filter')
.wait(500)
.evaluate(function(){
return document.querySelector('html').innerHTML;
})
.end()
.then(function (result) {
defer.resolve(result);
})
.catch(function (error) {
debug('ERROR >> Search failed:', error);
});
break;
}
}
});
});
return defer.promise;
}