网页抓取数据

web scraping for data

我得到一个

referenceError: Terminal A is not defined

第 48 行。我不确定我做错了什么。

writeStream.write(`Terminal A,Terminal B,Terminal C/D \n`);


var minutes = 1, timerInerval = minutes * 60 * 1000;


function TerminalOccupancyData() {

 
  request('https://www.laguardiaairport.com/to-from-airport/parking', (error, response, html) => {
    // Check there is no error
    if (!error && response.statusCode == 200) {
      // using cheerio library to load the website page html
      const $ = cheerio.load(html);

      $('.terminal-left').each((span, el) => {
        // Find the element using the class
        const terminalA = $(el)
          .find('.terminal-percentage')
          .text()
          .replace(/% Full/, '');

        const terminalB = $(el)
          .find('.terminal-percentage')
          .text()
          .replace(/% Full/, '');

        const terminalCD = $(el)
          .find('.terminal-percentage')
          .text()
          .replace(/% Full/, '');
      });

      console.log('\nTerminal Data scraped ... \n');
    }

  });

  // Export to file to upload to database
  writeStream.write(`${terminalA},${terminalB},${terminalCD} \n`);
}

setInterval(TerminalOccupancyData, timerInerval);

您在这里遇到的问题是您的 writeStream.write(...) 与其引用的变量不在同一范围内。由于您没有尝试 return 来自外部函数的任何值,我相信可以解决您的问题的方法只是将该语句移动到 .each() 回调函数中,如下所示:

function TerminalOccupancyData() {

 
  request('https://www.laguardiaairport.com/to-from-airport/parking', (error, response, html) => {
    // Check there is no error
    if (!error && response.statusCode == 200) {
      // using cheerio library to load the website page html
      const $ = cheerio.load(html);

      $('.terminal-left').each((span, el) => {
        // Find the element using the class
        const terminalA = $(el)
          .find('.terminal-percentage')
          .text()
          .replace(/% Full/, '');

        const terminalB = $(el)
          .find('.terminal-percentage')
          .text()
          .replace(/% Full/, '');

        const terminalCD = $(el)
          .find('.terminal-percentage')
          .text()
          .replace(/% Full/, '');

          // Export to file to upload to database
          writeStream.write(`${terminalA},${terminalB},${terminalCD} \n`);
      });

      console.log('\nTerminal Data scraped ... \n');

    }

  });

  
}

setInterval(TerminalOccupancyData, timerInerval);