Search code examples
javascriptvbaweb-scrapinggetelementsbyclassname

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.


Solution

  • 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