You need to create a HTMLDocument object from the response and use it for the parsing. As annotated in the code, it is necessary to use early binding to use the method getElementsByClassName
.
Try something like the following:
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
Alternatively, you can use only the tags and check for the class name in a loop for the parsing. The advantage is, that this method will also work with a late bound 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