单击函数未执行正在执行 Nightmare.js
Click function not executing executing Nightmare.js
我正在尝试使用 nightmare
进行一些抓取,我的工作几乎可以正常工作。问题是我在调用 evaluate()
和 run()
后尝试执行 click()
时遇到问题。在我 运行 这两个功能之后,我试图执行另一次单击以将自己移动到网站的另一部分,但没有执行 click()
。
此时我确定是什么问题,我几乎没有假设,也许这些函数是异步的,我正在尝试 click()
当回调尚未准备好或其中一个函数结束时de current nightmare
对象,我不再有范围了。
var Nightmare = require('nightmare');
//var nightmare = Nightmare({show:true})
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
var urlWeb = "someurl";
var selectCity = "#ddl_city";
var selectTheater = "#ddl_theater";
var enterBtn = "#btn_enter";
var mainSelector = "#aspnetForm";
var flagReady = true;
new Nightmare({show:true})
.goto(urlWeb)
.wait(selectCity)
.select(selectCity, '19')
.wait(8000)
.select(selectTheater, '12')
.wait(1000)
.click(enterBtn)
.wait(mainSelector)
.evaluate(function(){
//returning HTML for cheerio
return document.body.innerHTML;
})
.run(function(err, nightmare){
if (err) return console.log(err);
// Loading HTML body on jquery cheerio
var $ = cheerio.load(nightmare);
//Looping on each div for seccion de Carterla para Hoy
$('.showtimeDaily').each(function(index, element){
//spanish title
console.log($(this).find('h3').children().text());
//english title
console.log($(this).find('h4').text());
//schedule for today
console.log($(this).find('li').children().text() + " ");
//img for movie
console.log($(this).find('img').attr('src'));
//show time data such as gender, lenght, language
console.log($(this).find('.showtimeData').text());
var showtimeData = $(this).find('.showtimeData').text();
//console.log(JSON.stringify(showtimeData.replace(/\t|\n/g, "")));
})
console.log('Done!');
})
//*****here is wen I try to click*****
.click('a[href="../showtimes/weekly.aspx"]');
我遇到了异步回调的问题,所以我所做的是嵌套调用噩梦对象以确保任务 运行 一个接一个地执行。这是代码:
nightmare
.goto(urlWeb)
.wait(selectCity)
.select(selectCity, '19')
.wait(8000)
.select(selectTheater, '12')
.wait(1000)
.click(enterBtn)
.wait(mainSelector)
.evaluate(function(){
//returning HTML for cheerio
return document.body.innerHTML;
})
.then(function(body){
// Loading HTML body on jquery cheerio
var $ = cheerio.load(body);
//Looping on each div for seccion de Carterla para Hoy
$('.showtimeDaily').each(function(index, element){
//spanish title
console.log($(this).find('h3').children().text());
//english title
console.log($(this).find('h4').text());
//schedule for today
console.log($(this).find('li').children().text() + " ");
//img for movie
console.log($(this).find('img').attr('src'));
//show time data such as gender, lenght, language
console.log($(this).find('.showtimeData').text());
var showtimeData = $(this).find('.showtimeData').text();
//console.log(JSON.stringify(showtimeData.replace(/\t|\n/g, "")));
})
//**Here I call nightmare to run after the first call back is done*****
nightmare
.goto('')
.wait('body')
.title()
.then(function(title){
console.log(title);
});
console.log('Done!');
});
我正在尝试使用 nightmare
进行一些抓取,我的工作几乎可以正常工作。问题是我在调用 evaluate()
和 run()
后尝试执行 click()
时遇到问题。在我 运行 这两个功能之后,我试图执行另一次单击以将自己移动到网站的另一部分,但没有执行 click()
。
此时我确定是什么问题,我几乎没有假设,也许这些函数是异步的,我正在尝试 click()
当回调尚未准备好或其中一个函数结束时de current nightmare
对象,我不再有范围了。
var Nightmare = require('nightmare');
//var nightmare = Nightmare({show:true})
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
var urlWeb = "someurl";
var selectCity = "#ddl_city";
var selectTheater = "#ddl_theater";
var enterBtn = "#btn_enter";
var mainSelector = "#aspnetForm";
var flagReady = true;
new Nightmare({show:true})
.goto(urlWeb)
.wait(selectCity)
.select(selectCity, '19')
.wait(8000)
.select(selectTheater, '12')
.wait(1000)
.click(enterBtn)
.wait(mainSelector)
.evaluate(function(){
//returning HTML for cheerio
return document.body.innerHTML;
})
.run(function(err, nightmare){
if (err) return console.log(err);
// Loading HTML body on jquery cheerio
var $ = cheerio.load(nightmare);
//Looping on each div for seccion de Carterla para Hoy
$('.showtimeDaily').each(function(index, element){
//spanish title
console.log($(this).find('h3').children().text());
//english title
console.log($(this).find('h4').text());
//schedule for today
console.log($(this).find('li').children().text() + " ");
//img for movie
console.log($(this).find('img').attr('src'));
//show time data such as gender, lenght, language
console.log($(this).find('.showtimeData').text());
var showtimeData = $(this).find('.showtimeData').text();
//console.log(JSON.stringify(showtimeData.replace(/\t|\n/g, "")));
})
console.log('Done!');
})
//*****here is wen I try to click*****
.click('a[href="../showtimes/weekly.aspx"]');
我遇到了异步回调的问题,所以我所做的是嵌套调用噩梦对象以确保任务 运行 一个接一个地执行。这是代码:
nightmare
.goto(urlWeb)
.wait(selectCity)
.select(selectCity, '19')
.wait(8000)
.select(selectTheater, '12')
.wait(1000)
.click(enterBtn)
.wait(mainSelector)
.evaluate(function(){
//returning HTML for cheerio
return document.body.innerHTML;
})
.then(function(body){
// Loading HTML body on jquery cheerio
var $ = cheerio.load(body);
//Looping on each div for seccion de Carterla para Hoy
$('.showtimeDaily').each(function(index, element){
//spanish title
console.log($(this).find('h3').children().text());
//english title
console.log($(this).find('h4').text());
//schedule for today
console.log($(this).find('li').children().text() + " ");
//img for movie
console.log($(this).find('img').attr('src'));
//show time data such as gender, lenght, language
console.log($(this).find('.showtimeData').text());
var showtimeData = $(this).find('.showtimeData').text();
//console.log(JSON.stringify(showtimeData.replace(/\t|\n/g, "")));
})
//**Here I call nightmare to run after the first call back is done*****
nightmare
.goto('')
.wait('body')
.title()
.then(function(title){
console.log(title);
});
console.log('Done!');
});