gpt4 book ai didi

html - 如何使用 "getElementByClassName()"解析 VBA 中的 HTML 元素?

转载 作者:行者123 更新时间:2023-12-04 22:19:40 24 4
gpt4 key购买 nike

我将从 HTML 中获取元素的值。
HTML 代码如下。
它可以显示在屏幕上。
Code screenshot

Dim xmlhttp As Object
Dim url As String
Dim toTranslate As String
Dim htmldoc As HTMLDocument


toTranslate = TextBox1.Value
url = "http://dict.youdao.com/search?q=" & toTranslate & "&keyfrom=dict.index"
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") '创建XML对象

xmlhttp.Open "GET", url, False '用GET 发送请求

xmlhttp.send
'等待响应
Do While xmlhttp.readyState <> 4
DoEvents
Loop
Dim explore As New InternetExplorer


'Set htmldoc =explore.document

MsgBox xmlhttp.responseText
但是我想在类是“trans-container”(每个中文单词)的元素中获取标签“li”的每个值。
The content I want to get
我只知道“getElementsByClassName()”这个方法,但是不知道怎么用。谢谢你的帮助!

最佳答案

您需要从响应中创建一个 HTMLDocument 对象并将其用于解析。如代码中注释的,需要使用早期绑定(bind)才能使用方法getElementsByClassName .
尝试以下操作:

Dim url As String
Dim toTranslate As String

toTranslate = TextBox1.Value
' Note: use https:// rather than http://
url = "https://dict.youdao.com/search?q=" & toTranslate & "&keyfrom=dict.index"

' Creating and sending the request:
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") '创建XML对象

xmlhttp.Open "GET", url, False '用GET 发送请求
xmlhttp.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
xmlhttp.send ""

' Getting the response
' This needs to be early bound so the method getElementsByClassName is available!
' The required reference is "Microsoft HTML Object Library"
Dim objHTML as HTMLDocument
Set objHTML = New HTMLDocument

objHTML.body.innerHTML = xmlhttp.responseText

' Parsing the response
Dim objTransContainers as Object, objTransContainer as Object
Dim objLis as Object, objLi as Object
Dim retText as String

Set objTransContainers = objHTML.getElementsByClassName("trans-container")

For Each objTransContainer in objTransContainers
Set objLis = objTransContainer.getElementsByTagName("li")
For Each objLi in objLis
retText = retText & objLi.innerText & vbNewLine
Next objLi
Next objTransContainer

MsgBox retText
或者,您可以只使用标签并在循环中检查类名以进行解析。优点是,此方法也适用于后期绑定(bind)的 HTMLDocument:
' Getting the response:
Dim objHTML as Object
Set objHTML = CreateObject("htmlFile")

' Note: this objHTML.write will not work with early binding!
' In that case you have to use the .body.innerHTML
' like in the code sample above.
With objHTML
.Open
.write xmlhttp.responseText
.Close
End With

' Parsing the response
Dim objDivs as Object, objDiv as Object
Dim objLis as Object, objLi as Object
Dim retText as String

Set objDivs = objHTML.getElementsByTagName("div")

For Each objDiv in objDivs
If objDiv.className = "trans-container" Then
Set objLis = objDiv.getElementsByTagName("li")
For Each objLi in objLis
retText = retText & objLi.innerText & vbNewLine
Next objLi
End If
Next objDiv

MsgBox retText

关于html - 如何使用 "getElementByClassName()"解析 VBA 中的 HTML 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65157739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com