0

Basically i want to have an update content of the web, https://www.investing.com/indices/indices-futures source

In Dow 30, the last value is updating itself(real-time update from Investing.com server) and i would like to know is there any method to capture the change of values without requesting the website again, so that i can update on my code asynchronously. Since all i found online about being notified on change is based on their own html, but in my case it is external url, so i am asking here to gain some insight

Edison Lo
  • 476
  • 8
  • 25
  • You want to get updated data from a site without requesting updated data from a site? Unless they offer a push notification API service, the answer is no. – Dave S Mar 05 '19 at 21:56
  • @DaveS yes be able to observe the change without calling another http request – Edison Lo Mar 05 '19 at 21:58

2 Answers2

1

You can add some code into the chrome console and track this value every second to notify you.

let last_value = -1
let class_selector = 'pid-8873-last'
setInterval(function() {
  let v = document.getElementsByClassName(class_selector)[0].innerText
  if (v != last_value) {
    console.log("Value as been updated to " + v)
    last_value = v
  }
}, 1000)

> Value as been updated to 25,799.5
> Value as been updated to 25,798.5

But you must have a browser open, and create an ajax request when value is updated. If you don't want any Browser, but be run into a server, you can check PhantomJS


EDIT WITH PHANTOMJS

They're some update to do to work with PhantomJS.

  • You need to replace let by var
  • document isn't accessible, so you need to use evaluate
  • https may require to add --ssl-protocol=any

./test.js

var page = require('webpage').create();
page.open('https://www.investing.com/indices/indices-futures', function(status) {
  var last_value = -1

  setInterval(function() {
    var value = page.evaluate(function() {
      return document.getElementsByClassName('pid-8873-last')[0].innerText
    })

    if (value != last_value) {
      console.log("Value as been updated to " + value)
      last_value = value
    }
  }, 1000)
//  phantom.exit()
})

Then run it from the same directory:

# phantomjs test.js 
Value as been updated to 25,799.0
Community
  • 1
  • 1
Arthur
  • 4,870
  • 3
  • 32
  • 57
  • the console code works fine, but when i tried on PhantomJS, and add the code to function open(url, function (status) {}, it doesnt work, any ideas on where to put the code in PhantomJS? – Edison Lo Mar 05 '19 at 22:28
  • your current answer works great, but at site such as http://www.aastocks.com/tc/stocks/market/bmpfutures.aspx with class id(div): font26 bold cls ff-arial, it seems the phantomjs is not refreshing the data? i have tried to clear the cache but it still does not work out. Any idea? – Edison Lo May 27 '19 at 06:16
0

I think you need to check what is websocket. this would be cool start; https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

quinkan
  • 1
  • 1
  • Websocket works fine but you need a server to contact and who send to you the data when it's updated.. On this case the website have no api – Arthur May 27 '19 at 12:55