如何解决Node.js 噩梦,Cheerio 没有写入控制台
我想弄清楚为什么我的脚本没有将控制台日志输出到...控制台。
const Nightmare = require('nightmare')
const cheerio = require('cheerio');
const fs = require('fs');
// Consts
const url = 'https://www.mscl.org/live/scorecard/ed7941919f69b0e11e800fef/mHcehsPR9S86T3zQv';
const interval = 10*1000; // every 10 second
let mvccLiveScore = {
battingTeam: '',score: 0,lastChecked: 0
};
let getData = html => {
const $ = cheerio.load(html);
$('div').each((i,elem) => {
if( i === 0 ){
mvccLiveScore.battingTeam = $(elem).find('div.row:nth-child(1) div:nth-child(1) h5').html();
mvccLiveScore.score = $(elem).find('div.row:nth-child(1) div:nth-child(2) h5').html();
mvccLiveScore.lastChecked = new Date().toLocaleString();
console.log(mvccLiveScore);
}
});
let data = JSON.stringify(mvccLiveScore);
fs.writeFileSync('mvccLiveScore.json',data);
}
function runMvccScrapper(){
console.log('Starting the scrapping');
const nightmare = Nightmare({ show: true })
nightmare
.goto(url)
.wait('body')
.wait('div#summaryTab.tab-pane.fade.in.table-responsive.borderless.active')
.evaluate(() => document.querySelector('div.container-fluid.liveMatchSummaryTab div.row div.col-md-6 div.container-fluid').innerHTML)
.end()
.then(response => {
getData(response);
}).catch(err => {
console.log(err);
}).then(()=>{
console.log('Scrapping is done');
console.log('/****************************/');
});
}
setInterval(function(){
console.log('/****************************/');
console.log('Time to run the job');
runMvccScrapper();
},interval);
这个脚本看起来很简单。打印到控制台的唯一内容是“运行作业的时间”,然后是 console.log('Starting the scrapping'); 就像噩梦根本不起作用或运行一样。
我在这里错过了什么?为什么这不起作用?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。