Search code examples
node.jscsvweb-scrapingexport-to-csv

web scraping for data


I get a

referenceError: Terminal A is not defined

on line 48. I'm not sure what I'm doing wrong.

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);

Solution

  • The issue you're having here is that your writeStream.write(...) is not inside of the same scope as the variables it's referencing. Since you're not trying to return any values from the outer function, I believe what will solve your problem is simply moving that statement inside the .each() callback function, like so:

    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);