gpt4 book ai didi

VBA,MSXML2.XMLHTTP60 在头部解析

转载 作者:行者123 更新时间:2023-12-02 21:18:01 26 4
gpt4 key购买 nike

我用它来获取网页

Dim xhr As MSXML2.XMLHTTP60
Set xhr = New MSXML2.XMLHTTP60

On Error Resume Next

With xhr

.Open "GET", URL, False
.send

If .ReadyState = 4 And .Status = 200 Then
Set doc = New MSHTML.HTMLDocument
doc.body.innerHTML = .responseText

htmlRequestHTTP = True
Else
MsgBox "Internet Error, please check:" & vbNewLine & "Ready state: " & .ReadyState & _
vbNewLine & "HTTP request status: " & .Status
htmlRequestHTTP = False
End If

End With

但是 doc.getElementsByTagName("META") 错过了 HEAD 部分的标签。现在 .response 已完成(我检查过),那么如何访问 HEAD 元素?谢谢,

最佳答案

不要写入文档正文。写入文档本身。

Set doc = New MSHTML.HTMLDocument
doc.write HttpGet(URL)
MsgBox doc.getElementsByTagName("META").length

辅助函数HttpGet:

Function HttpGet(url) As String
With New MSXML2.XMLHTTP60
.open "GET", url, False
On Error Resume Next
.send
If .Status = 200 Then
HttpGet = .responseText
Else
MsgBox "HTTP request status: " & .Status, , "HttpGet Error"
End If
On Error GoTo 0
End With
End Function

作为一般提示,请勿将 On Error Resume Next except 用于下一条语句。

理想情况下,将可能失败的语句包装在专用函数中,以将 On Error Resume Next 覆盖的代码区域保持在绝对最小值,就像我所做的那样。

关于VBA,MSXML2.XMLHTTP60 在头部解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29322374/

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