Jiri Zaloudek
Jiri Zaloudek

Reputation: 380

How to get value from website javascript?

On the webpage I have this:

<table class="infobox"><tr>
<td>
<table class="infobox-inner-table">
<tr class="infobox-heading">
<th id="infobox-quick-facts">Quick Facts</th>
</tr>
<tr><td>
<div class="infobox-spacer"></div>
<div id="infobox-contents-0"></div>
<script>
      WH.markup.printHtml("[ul][li]Requires level 20[\/li][li]Loremaster: Yes[\/li][li]Side: [span class=icon-alliance]Alliance[\/span][\/li][li][icon name=quest_start]Start: [url=\/npc=41129\/surveyor-thurdan]Surveyor Thurdan[\/url][\/icon][\/li][li][icon name=quest_end]End: [url=\/npc=41129\/surveyor-thurdan]Surveyor Thurdan[\/url][\/icon][\/li][li]Sharable[\/li][li]Added in patch 4.0.3.13277[\/li][\/ul]", "infobox-contents-0", {
                allow: WH.markup.CLASS.STAFF,
                dbPage: true,            });
        </script>
</td></tr>
</table>

Inside the javascript is "Added in patch 4.0.3.13277" and via VBA I have to get the patch number.

Best would be to use getelementsbyclassname("infobox") so it will only look to this , however then I don't know what to do next, .innerText or anything similar to dig up the patch number doesn't apply here.

Upvotes: 0

Views: 79

Answers (2)

MITHU
MITHU

Reputation: 154

Why don't you directly look around the script to dig out the patch number? This is how I meant:

Sub FetchPatchNumber()
    Const Url$ = "https://stackoverflow.com/questions/61192812/vba-how-to-getvalue-from-website-javascript"
    Dim Http As New XMLHTTP60, patchnum As Object, S$

    With Http
        .Open "GET", Url, False
        .send
        S = .responseText
    End With

    With CreateObject("VBScript.RegExp")
        .Pattern = "Added in patch\s*(.*?)\["
        Set patchnum = .Execute(S)
        If patchnum.Count > 0 Then
            MsgBox patchnum.item(0).SubMatches(0)
         End If
    End With
End Sub

Upvotes: 2

QHarr
QHarr

Reputation: 84465

You can regex out the appropriate script contents then replace the \/ with / ; replace [ with < ; replace ] with > ; then parse with html parser and grab last li element.

Option Explicit

Public Sub GetTextFromScriptTag()
    'required references Microsoft HTML Object Library; Microsoft VBScript Regular Expressions

    'your code

    Dim html As MSHTML.HTMLDocument, re As VBScript_RegExp_55.RegExp

    'Set html = htmlsourceobject(e.g.ie.document) ''< this line you need to add in html source object from your prior code
    Set re = New VBScript_RegExp_55.RegExp

    re.Pattern = "WH\.markup\.printHtml\(""(.*?)"","

    html.body.innerHTML = "<body>" & Replace$(Replace$(Replace$(re.Execute(html.body.innerHTML)(0).SubMatches(0), "[", "<"), "]", ">"), "\/", "/") & "<\body>"

    Dim liNodes As Object

    Set liNodes = html.querySelectorAll("li")
    Debug.Print liNodes.item(liNodes.Length - 1).innerText

End Sub

Regex:

enter image description here

Upvotes: 2

Related Questions