我用它来获取网页
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元素? 谢谢,
答案 0 :(得分:3)
不要写信给文件正文。写入文档本身。
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
作为下一个声明。
理想情况下,在专用函数中包含失败的语句,以便将On Error Resume Next
所涵盖的代码区域保持在绝对最小值,就像我一样。