【问题标题】:How to get value from website javascript?如何从网站 javascript 中获取价值?
【发布时间】:2020-07-26 07:15:49
【问题描述】:

在网页上我有这个:

<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 或类似挖掘补丁号的东西在这里不适用.

【问题讨论】:

  • 这些解决方案是否适合您?
  • 你好 QHarr,在这种情况下,我使用了 MITHU 的解决方案,因为它更容易理解,看起来更清晰,可能更快,因为它很适合我需要更好地使用的方式和位置。但是对于我在项目中的所有其他问题,我只使用了您的解决方案,因为它们非常简单,不复杂并且效果很好
  • 当然 :-) 请记住考虑接受他们的回答。 @Mithu 的解决方案简洁明了。使用适合您项目的任何东西。

标签: javascript vba web-scraping getelementsbyclassname


【解决方案1】:

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

正则表达式:

【讨论】:

  • 在不替换 HTML \[li\](.+?)\[ 的情况下使用这样的模式是否不足以获得结果?
  • 随着时间的推移,IMO 的模式可能会发生变化。我认为最好抓住注定要被解析为 html 的整个部分,因为它可能更稳定。我正在假设位置而不是字符串。如果我确定前面的文字,例如添加了补丁,然后我可能会使用像 @Mithu 这样的正则表达式,但首先使用脚本之前的 id 和脚本标记来定位节点。
【解决方案2】:

为什么不直接环顾一下脚本,找出补丁号呢?这就是我的意思:

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

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    相关资源
    最近更新 更多