在网页上,我有这个:
<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>
[在javascript内部是“在4.0.3.13277补丁中添加的”,并且必须通过VBA获得补丁号。
最好是使用getelementsbyclassname(“ infobox”),因此它只会查看此内容,但是随后我不知道下一步该怎么做。
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
正则表达式:
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