HtmlAgilityPack - how to get Innerhtml value in function{}

Viewed 352

I am trying to parse a website which has Following html lines

<script type="text/javascript">
$(document).ready(function() {
          set_rate()
});
function set_rate() {
    var rate_f = "1";
    $('#rate_c').html("1.10");
    $('#rate_g').html("7.00");
    $('#time').html("23/08/2021 Evening 02:31 PM");
}

I have no knowledge of html so I am lost at pointing HtmlAgilityPack to correct node.

I am trying to get $('#rate_c').html("1.10"); value 1.10, $('#rate_g').html("7.00"); value 7.00 and $('#time').html("23/08/2021 Evening 02:31 PM"); value 23/08/2021 Evening 02:31 PM

I am not trying to get values the function gives after running JavaScript, lucky for me the html has the value I am looking for.

1 Answers

Based on your comments in chat, you can try doing this:

Dim result = New With {.rate_c = "", .rate_g = "", .time = ""}

Dim doc As New HtmlAgilityPack.HtmlDocument
doc.Load("C:\Temp\test.html")

Dim scriptNodes = doc.DocumentNode.SelectNodes("//script")
For Each scriptNode In scriptNodes
    If scriptNode.InnerHtml.Contains("$('#rate_c').html(") Then
        result.rate_c = scriptNode.InnerHtml.Substring(scriptNode.InnerHtml.IndexOf("$('#rate_c').html(") + "$('#rate_c').html(".Length + 1)
        result.rate_c = result.rate_c.Substring(0, result.rate_c.IndexOf(""""))
    End If
    If scriptNode.InnerHtml.Contains("$('#rate_g').html(") Then
        result.rate_g = scriptNode.InnerHtml.Substring(scriptNode.InnerHtml.IndexOf("$('#rate_g').html(") + "$('#rate_g').html(".Length + 1)
        result.rate_g = result.rate_g.Substring(0, result.rate_g.IndexOf(""""))
    End If

    '--- ... similarly get other values here ...

Next

'-- lets see what we got!
MessageBox.Show($"rate_c={ result.rate_c}    rate_g={result.rate_g}")
Related