Tema: Re: programiskai istraukti reiksme is tinklapio (su linux)
Autorius: Laimis
Data: 2017-02-01 19:19:19
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Herkus rašė:
> super, aciu!

Na ir ateičiai, sudėtingesniais atvejais, kai public API lengvai
neprieinamas:

phantomjs:
http://phantomjs.org/


> cat temp.js
var page = require('webpage').create();
page.open('https://www.windguru.cz/station/244', function(status) {
  if(status !== "success") {
	console.log('Unable to access network');
	phantom.exit(1);
  }
});


function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //<
Default Max Timout is 3s
        start = new Date().getTime(),
        condition = false,
        interval = setInterval(function() {
            if ( (new Date().getTime() - start < maxtimeOutMillis) &&
!condition ) {
                // If not time-out yet and condition not yet fulfilled
                condition = (typeof(testFx) === "string" ?
eval(testFx) : testFx()); //< defensive code
            } else {
                if(!condition) {
                    // If condition still not fulfilled (timeout but
condition is 'false')
                    console.log("'waitFor()' timeout");
                    phantom.exit(1);
                } else {
                    // Condition fulfilled (timeout and/or condition
is 'true')
                    //console.log("'waitFor()' finished in " + (new
Date().getTime() - start) + "ms.");
                    typeof(onReady) === "string" ? eval(onReady) :
onReady(condition); //< Do what it's supposed to do once the condition
is fulfilled
                    clearInterval(interval); //< Stop this interval
                }
            }
        }, 250); //< repeat check every 250ms
};

page.onLoadFinished = function() {
	waitFor(function() {
    	// Check in the page if a specific element is now visible
	    return page.evaluate(function() {
    	    return
document.getElementsByClassName('wgs_temp_value')[0].textContent;
        	});
        }, function(condition) {
	        console.log("Temp: " + condition);
    	    phantom.exit();
	});
};


> phantomjs temp.js
Temp: -5.4


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJYkhiXAAoJEIGZp48fbGsmVBoQAJT45cdturBdgnMBMwclqN3G
AYIY8OgUiqyfEPpPnEvGK9GshS/J6Fp/yKyScvMAWGwDT+f7wtCGvHrNJSt6wfb+
unuyBSqh4Qw8R+T+lHECz4VRSk7bogr67V3PKT83wN1dAL26b6BygoDauLMhRGBu
0MQqnc9QPwuRKcJ07FBDn3I04PtOXukN6m/vgfW5D2z1ahly8eGOrBmRMez7jsxg
dWyL5PX6Oh6NfGKgVnNbYnXkLbA89GCuauozJWYnBfaru9Xm2Ti634KpZCA2kDPo
Vssbx+2u9h/+dBKQoOx0ooczltimYPmVJjavXgXfXuZnLpU3tJL1N+kUdUKQy4at
tUeZJjCJ4f/Uzo9XYg40pRnuWAUh1Oh55WakJ4cn0ot0Jytto+poUbSitWrSx2IK
z14U/43f9j2BTQBlImNrycVRvSBygo+UjsG8ZGu4EVxAxwMxs2mV0ZglsxrY1rV0
p1Sq+IpYGXwOoms4tdsTIaSp1D3opPL6IAiMrajY+HggD+iCD4hRK1nxjnKnYCO3
gphLcWCy13RHhMfAbHEx63KRoDfEW1icPaYvgl54n51gmKIGw0OSgiiTwEFYseY2
n6wfQBwGxhehTqa9z7HQHHcxVPWxvYCBLmoOSK6bIwLDMsX81iBgrZXQeaV8mrEQ
T892oNdEx+hlXoHQcOuP
=9aOh
-----END PGP SIGNATURE-----