Search code examples
node.jszombie.js

Zombie.js check dynamic updates


I am trying to scrape content from a web page that is continuously changing. I have been able to use PhantomJS to achieve this however wanted a lighter weight solution. The following code gets the correct value the first time it prints to the console. However on following iterations the same value is printed. Any ideas?

var Browser = require("zombie");
var assert = require("assert");

// Load the page from localhost
browser = new Browser()
browser.visit("http://www.timeanddate.com/worldclock/usa/los-angeles", function () { 

  setInterval(function(){
    console.log(browser.text('#ct'));
  },10000);
});

Note the example above is purely an example. I know this would be the most inefficient way to get the time in Los Angeles.


Solution

  • Once you call browser.visit(), the browser stores the response, but unless you call it multiple times, the response won't change. See it for yourself:

    browser.visit("http://www.timeanddate.com/worldclock/usa/los-angeles", function () { 
      console.log(browser.html()); // will print the HTML to stdout
    });
    

    So what you probably want is to call browser.visit() more than once, maybe inside setInterval() (although there may be more robust solutions out there).

    I readapted your code:

    var Browser = require("zombie");
    var assert = require("assert");
    
    var browser = new Browser();
    
    setInterval(function () {
      browser.visit("http://www.timeanddate.com/worldclock/usa/los-angeles", function () {
        console.log(browser.text('#ct'));
      });
    }, 10000);