How to get json objects embedded in html page result of js running by phantomJS and pass them to java code?

Question

I use js script code that described in this answer, but i don't want to save html result page in html file. I want to extract Json object from <div class="rg_meta"> and pass them to java code. In searching , I find using "document", but i get undefined error. Any help as soon as possible in writing script and java code would be appreciated(I am newbie in phantomJS and working with JSON in java).

var page = require('webpage').create();
var fs = require('fs');
var system = require('system');

var url = "";
var searchParameter = "";
var count=0;

if (system.args.length === 4) {
    url=system.args[1];
    searchParameter=system.args[2];
    count=system.args[3];
}

if(url==="" || searchParameter===""){
    phantom.exit();
}

page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';

page.zoomFactor = 0.1;

page.viewportSize = {
  width: 1920,
  height: 1080
};

var divCount="-1";
var topPosition=0;
var unchangedCounter=0;


page.open(url, function(status) {
console.log("Status: " + status);
if(status === "success") {

    window.setInterval(function() {

        var newDivCount = page.evaluate(function() { 
            var divs = document.querySelectorAll(".rg_di.rg_bx.rg_el.ivg-i");
            return divs[divs.length-1].getAttribute("data-ri");
        });

        topPosition = topPosition + 1080;

        page.scrollPosition = {
            top: topPosition,
            left: 0
        };

        if(newDivCount===divCount){
            page.evaluate(function() {
                var elems=document.getElementByClassName("rg_meta");
                console.log(elems.length);
                var button = document.querySelector("#smb");
                if(!(typeof button === "undefined")) {
                    button.click();
                    console.log('Clicked');
                    return true;
                }else{
                    return false;
                }
            });

            if(parseInt(unchangedCounter,10) === parseInt(count,10)){
               /* var path = searchParameter+'.html';
                fs.write('seedHtml/'+path, page.content, 'w');
                console.log('printing html');*/
                phantom.exit();
            }else{
                unchangedCounter=unchangedCounter+1;
            }
        }else{
            unchangedCounter=0;
        }
        divCount = newDivCount;

    }, 500);
}else{
    phantom.exit();
}
});

Show source
| javascript   | java   | json   | parameter-passing   | phantomjs   2017-01-07 11:01 0 Answers

Answers to How to get json objects embedded in html page result of js running by phantomJS and pass them to java code? ( 0 )

Leave a reply to - How to get json objects embedded in html page result of js running by phantomJS and pass them to java code?

◀ Go back