Parsing HTML to get script variable value

Active3 hr before
Viewed126 times

7 Answers


Very simple example of how this could be easy using a HTMLAgilityPack and Jurassic library to evaluate the result:,I know this question may have been asked, but the responses suggest using something like HTMLAgilityPack, and I'd rather avoid using a library for this task as I only need to get the JavaScript from the DOM once.,Note: I am not accounting for errors or anything else, this merely serves as an example of how to grab the script and evaluate for the value of spect., 2 that's exactly why you use it,with 2 or 3 lines of code you can extract that script content with HTMLAgilityPack and then just parse it with a regex or even a JavaScript evaluator to get the value of spect which would simplify the entire work of parsing it with a regex. – Prix Aug 9 '13 at 22:51

Very simple example of how this could be easy using a HTMLAgilityPack and Jurassic library to evaluate the result:

var html = @"<html>
             // Some HTML
               var spect = [['temper', 'init', []],
               ['fw\/lib', 'init', [{staticRoot: '//'}]],
             // More HTML

// Grab the content of the first script element
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
var script = doc.DocumentNode.Descendants()
                             .Where(n => n.Name == "script")

// Return the data of spect and stringify it into a proper JSON object
var engine = new Jurassic.ScriptEngine();
var result = engine.Evaluate("(function() { " + script + " return spect; })()");
var json = JSONObject.Stringify(engine, result);


var parser = new DOMParser();
var htmlDoc = parser.parseFromString(txt, 'text/html');
load more v

A variable declared without a value will have the value undefined.,JavaScript variables are containers for storing data values.,Variables are containers for storing data (values).,The variable carName will have the value undefined after the execution of this statement:

var = "";

keepScripts (default: false) Type: Boolean A Boolean indicating whether to include scripts passed in the HTML string ,Description: Parses a string into an array of DOM nodes.,Create an array of DOM nodes using an HTML string and insert it into a div., data Type: String HTML string to be parsed

<!doctype html><html lang="en"><head>  <meta charset="utf-8">  <title>jQuery.parseHTML demo</title>  <script src=""></script></head><body> <div id="log">  <h3>Content:</h3></div> <script>var $log = $( "#log" ),  str = "hello, <b>my name is</b> jQuery.",  html = $.parseHTML( str ),  nodeNames = []; // Append the parsed HTML$log.append( html ); // Gather the parsed HTML's node names$.each( html, function( i, el ) {  nodeNames[ i ] = "<li>" + el.nodeName + "</li>";}); // Insert the node names$log.append( "<h3>Node Names:</h3>" );$( "<ol></ol>" )  .append( nodeNames.join( "" ) )  .appendTo( $log );</script> </body></html>

Note: when using Fast HTML Parser in a Typescript project the minimum Typescript version supported is ^4.1.2.,Get unescaped text value of current node and its children. Like innerText. (slow for the first time),Get escpaed (as-it) text value of current node and its children. May have &amp; in it. (fast),Returns a reference to the next child node of the current element's parent.

npm install--save node - html - parser
load more v

It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, and have the browser refresh itself, passing specific variables back to the PHP script. The example below shows precisely how to do this -- it allows PHP code to capture screen height and width, something that is normally only possible on the client side. , Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables. , PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. Before reading these faqs, it's important you learn how to retrieve variables from external sources. The manual page on this topic includes many examples as well. , There are several stages for which encoding is important. Assuming that you have a string $data, which contains the string you want to pass on in a non-encoded way, these are the relevant stages:

<input type="image" src="image.gif" name="foo" />
load more v

As a basic example, below is a simple HTML parser that uses the HTMLParser class to print out start tags, end tags, and data as they are encountered:,The following class implements a parser that will be used to illustrate more examples:,html.parser — Simple HTML and XHTML parser Example HTML Parser Application HTMLParser Methods Examples ,The tag argument is the name of the tag converted to lower case.

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print("Encountered a start tag:", tag)

def handle_endtag(self, tag):
print("Encountered an end tag :", tag)

def handle_data(self, data):
print("Encountered some data :", data)

parser = MyHTMLParser()


   <h1>Parse me!</h1>

load more v

Other "parsing-value" queries related to "Parsing HTML to get script variable value"