如何从网站javascript获取价值?
How to get value from website javascript?
在网页上我有这个:
<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 里面是 "Added in patch 4.0.3.13277" 并且通过 VBA 我必须得到补丁号。
最好是使用 getelementsbyclassname("infobox") 所以它只会看这个,但是我不知道下一步该做什么,.innerText 或任何类似的挖掘补丁号的东西都没有'在这里申请。
您可以用正则表达式输出适当的脚本内容,然后将 \/
替换为 /
;将 [
替换为 <
;将 ]
替换为 >
;然后使用 html 解析器解析并获取最后一个 li
元素。
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$ = ""
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
在网页上我有这个:
<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 里面是 "Added in patch 4.0.3.13277" 并且通过 VBA 我必须得到补丁号。
最好是使用 getelementsbyclassname("infobox") 所以它只会看这个,但是我不知道下一步该做什么,.innerText 或任何类似的挖掘补丁号的东西都没有'在这里申请。
您可以用正则表达式输出适当的脚本内容,然后将 \/
替换为 /
;将 [
替换为 <
;将 ]
替换为 >
;然后使用 html 解析器解析并获取最后一个 li
元素。
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$ = ""
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