首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >web数据抓取

web数据抓取
EN

Stack Overflow用户
提问于 2021-11-17 23:12:10
回答 1查看 37关注 0票数 0

我得到了一个

referenceError:终端A未定义

在第48线。我不知道我做错了什么。

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-17 23:34:28

这里存在的问题是,您的writeStream.write(...)与它所引用的变量不在同一范围内。由于您不试图从外部函数返回任何值,我相信解决问题的方法就是将该语句移到.each()回调函数中,如下所示:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70012726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档