Parse HTML Content to get Field Value

Kiran Patil3
Giga Expert

Hi All,

I am receiving below response in one of the API. We need to parse below HTML Content and extract only Status and progress Info.

Expected output is:

status = Confirming subcription...
Progress = Your browser has JavaScript disabled. <i>To confirm a subscription via this page, your browser must have JavaScript enabled. To confirm your subscription without enabling JavaScript in your browser, use an Amazon SNS client to call ConfirmSubscription with the <code>Topic</code> and <code>Token</code> parameters supplied in the confirmation message.

How can I parse the given HTML and extract required info. Thanks in advance.

Thank You

 

<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>

<style>
html, body {
	margin: 0; padding: 0;
	background: #fff;
	color: #333;
	font: 12px/15px Verdana, sans-serif;
}
#container {
	max-width: 520px;
	margin: 30px;
	padding: 0px;
	
}
#content h2 {
	font: bold 16px/16px Verdana, sans-serif;
	margin: 0;
	padding: 0;
	margin-bottom: 12px;
}
#header {
	background: url("aws_logo_108x40.gif") top left no-repeat;
	height: 40px;
	padding-bottom: 0;
	color: #e47911;
	position:relative;
	margin-bottom: 12px;
}
#header h1 {
	font-size: 12px;
	line-height: 12px;
	margin: 0; padding: 0;
	position: absolute;
	bottom: 0;
	right: 0;
}
#content {
	padding: 12px;
	background: #ecf5fb;
	border: 1px solid #c9e1f4;
	-moz-border-radius: 10px;
	-webkit-border-radius: 10px;
	border-radius: 10px;
}
p {
	margin-top: 0;
	margin-bottom: 12px;
}
.error {
	color: #900;	
}
.success {
	color: #090;
}
code {
	font-style: normal;
	color: #000;
}
abbr {
	font-weight: bold;
}
a:visited, a:hover {
    color: #004b91;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>Simple Notification Service</h1>
</div>
<div id="content">
<h2 id="status">Confirming subcription...</h2>
<div id="progress">
<noscript><p>Your browser has JavaScript disabled. <i>To confirm a subscription via this page, your browser must have JavaScript enabled. To confirm your subscription without enabling JavaScript in your browser, use an Amazon SNS client to call ConfirmSubscription with the <code>Topic</code> and <code>Token</code> parameters supplied in the confirmation message.</p></noscript>
</div>
</div>
</div>

<script type="text/javascript">
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function validSnsArn(snsArn) {
    var arnMatcher = /^arn:aws[\-a-z]*:sns[\-a-z]*:[a-zA-Z]{2}[\-a-zA-z]+\-[0-9]+:[0-9]{12}:[a-zA-Z_0-9\-_]{1,256}$/;

    if (! arnMatcher.test(snsArn)) {
    	return false;

    } 
    return true;
}

$(document).ready(function(){
    var topic = getParameterByName("TopicArn");
    var friendlyTopic = topic.split(":")[5];
    var token = getParameterByName("Token");
    var endpoint = getParameterByName("Endpoint").replace(/%/g,"").replace(/</g,"").replace(/>/g,"");
    var serviceURL = getParameterByName("Service"); //testing purposes only
    
    var baseURL = window.location.host; 
    if (baseURL == null || baseURL == "") {
        baseURL = "https://aws.amazonaws.com";
    }
    if (serviceURL != ""){
        baseURL = serviceURL
    }
    var relativeURL = "/?Action=ConfirmSubscription&TopicArn=" + topic + "&Token=" + token;
    var confirmationURL = baseURL + relativeURL;
    var failureString = "<p>Your subscription could not be confirmed because of an error. To receive messages from the topic, please resubscribe your email address.</p>";

    var test = getParameterByName("Success");

    if (!validSnsArn(topic)){
    	test="";
    	topic="";
    }

   	if (test != ""){ 
        $("#status").html("Subscription confirmed!").addClass("success");
        $("#progress").html("<p>You have subscribed to the topic:<br /><abbr title=\"" 
                                            + topic 
                                            + "\">" 
                                            + friendlyTopic
                                            + "</abbr></p><p>Your subscription id is:<br /><code>arn:aws:sns:us-east-1:123456789012:test-topic:12345-12345-12345-123</code></p>");
    } else {
        if (topic == "" || token == "" ) {
            $("#status").html("Subscription <i>not</i> confirmed").addClass("error");
            $("#progress").html("<p>Your subscription could not be confirmed because your topic or token information is incomplete. Please make sure to use exactly the URL from the subscription confirmation message.</p>");
        } else {
            var response = $.ajax({ type: "GET", 
                                    url: relativeURL, 
                                    async: false,
                                    dataType: "xml",
                                    success: function(data, status, req){ 
                                        var sub = $("SubscriptionArn", data).text();
                                        $("#status").html("Subscription confirmed!").addClass("success");
                                        $("#progress").html("<p>You have subscribed " 
                                            + endpoint 
                                            + " to the topic:<br /><abbr title=\"" 
                                            + topic 
                                            + "\">" 
                                            + friendlyTopic
                                            + "</abbr>.</p><p>Your subscription's id is: <br /><code>" 
                                            + sub 
                                            + "</code></p><p>If it was not your intention to subscribe, <a href=\""
                                            + "/unsubscribe.html?SubscriptionArn="
                                            + sub
                                            + "\">click here to unsubscribe</a>.</p>");
                                    },
                                    error: function(req, status, error){
                                        $("#status").html("Subscription <i>not</i> confirmed").addClass("error");
                                        $("#progress").html(failureString); 
                                    } 
                                  });
        }
    }
});
</script>

</body>
</html>
1 REPLY 1

Inactive_Us1180
Kilo Guru

Looks like you are receiving html, css, and some script in the response... so because of that, you can try a Regular Expression... 

var response = <The response you are getting from the API>

var result = response.match(<h2 id="status">(.*)</h2>);

gs.log(result[1]);

 

This will grab the status, then just do the same thing just do the same for the progress text.