Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executing javascript selectors on page source in string format

I am trying to develop and application with jsoup and java to scrap some web pages. So what I am hoping to make is to let jsoup get the page source first and the on the page source let the below javascript get executed and return back a result.

$("body, body *").each(function(i, val) {
   // do something and something more
});

I am planning to use ScriptEngineManager to execute javascript code from Java.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");

My question is how can I make / is there a way that the JS script accept the Document(string) that jsoup returns and do select operation on it just like a regular page.

So what I am hoping to have is something like this:

    // document is the Document object returned from the Jsoup or Document converted to string
    var myfunction = function(document){
        document.$("body, body *").each(function(i, val) {
           // do something and something more
        });
    }
like image 439
ralzaul Avatar asked Aug 09 '26 02:08

ralzaul


1 Answers

jQuery selector accepts a string of html, and you can utilize the context/scope to select only from that variable. So you can do something like this as an example (using the variable as context):

//a string of elements/html
var doc = '<html><body><ul><li>1</li><li>2</li><li>3</li></ul></body></html>';
$('li', doc).each(function() {
    console.log(this); //iterates each 'li' within 'doc'
});

Or, in your case:

var myfunction = function(doc){
    $("*", doc).each(function(i, val) {
       // do something and something more
    });
}
like image 151
Mackan Avatar answered Aug 10 '26 16:08

Mackan