首页 > 解决方案 > 如何从网站 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里面是“在补丁4.0.3.13277中添加”,通过VBA我必须得到补丁号。

最好是使用 getelementsbyclassname("infobox") 所以它只会看这个,但是我不知道下一步该做什么, .innerText 或任何类似的挖掘补丁号的东西在这里不适用。

标签: javascriptvbaweb-scrapinggetelementsbyclassname

解决方案


您可以正则表达式输出适当的脚本内容,然后替换\//; 替换[<; 替换]>; 然后用 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

正则表达式:

在此处输入图像描述


推荐阅读