Thursday, 25 May 2017

Simple web scraping in node.js works on local side, but not sure how to run it on Lambda servers

I have the following code, which works when I run it on my machine:

var request = require('request');
var cheerio = require('cheerio');
var url = "https://www.google.com/search?q=current+date";

request(url, function(error, response, body) {
    if(!error) {
    console.log("Status code: " + response.statusCode);
    var $ = cheerio.load(body);
    date = $("div > div > div > div > span").text().trim();
    console.log("Date: " + date);
  } else {
     console.log("Error: " + error);
  }
});

It just grabs the current date from a google search as a simple data collection. However when I try to put it in a function for Alexa on a Lambda server:

'GetDate': function(){
    //this.emit(':tell', 'activated');
    var date;
    var url = "https://www.google.com/search?q=current+date";

    request(url, function(error, response, body) {
      if(!error) {
        var $ = cheerio.load(body);
        var date = $("div > div > div > div > span").text().trim();
        this.emit(':tell', 'Date: ' + date);
      } else {
        this.emit(':tell', 'Error connecting');
      } 

   });
 this.emit(':tell', 'Date: ' + date);
}, 

It fails to do anything, producing a response that says "Date: undefined". I think it may have something to do with making a connection, but I'm really not sure. It doesn't crash, but it also won't give me the date on Lambda, but it will when I run it from a command line on my local machine. I am thinking perhaps a .on('response') could help, but I'm not sure why the date isn't being define period, even if I just say date = 6 within the request or something. It's not crashing so I think I have all packages imported correctly.



via Noah L

No comments:

Post a Comment