我找到了一个代码,我将其转换为UDF以检查紫藤的网址是否有效。.
Sub Test()
MsgBox CheckValidURL("https://fast.wistia.net/embed/iframe/vud7ff4i6w")
End Sub
Function CheckValidURL(sURL As String) As Boolean
Dim oXMLHTTP As Object
Dim sResponseText As String
Dim aScriptParts As Variant
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
oXMLHTTP.Open "GET", sURL, False
oXMLHTTP.Send
sResponseText = oXMLHTTP.responseText
aScriptParts = Split(sResponseText, "<script", , vbTextCompare)
If UBound(aScriptParts) > 0 Then CheckValidURL = True
End Function
我已经使用多个链接测试了UDF,但是我得到了正确的结果,但是我不确定UDF是否正确 您可以建议我还是改进该UDF? 感谢高级帮助
答案 0 :(得分:2)
您可以通过在子对象中创建xhr对象并传递给函数来提高效率,然后仅查看响应标头link
进行区分
Option Explicit
Public Sub Test()
Dim urls(), i As Long, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
urls = Array("https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
For i = LBound(urls) To UBound(urls)
MsgBox CheckValidURL(urls(i), xhr)
Next
End Sub
Public Function CheckValidURL(ByVal url As String, ByVal xhr As Object) As Boolean
With xhr
.Open "GET", url, False
.send
CheckValidURL = Not .getResponseHeader("link") = vbNullString
End With
End Function
替代项:
在功能测试中是否存在仅在有效链接中的id或字符串(以您的方式)
Public Sub Test()
Dim urls(), i As Long, html As HTMLDocument, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP"): Set html = New HTMLDocument
urls = Array("https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
For i = LBound(urls) To UBound(urls)
MsgBox CheckValidURL(urls(i), xhr, html)
Next
End Sub
Public Function CheckValidURL(ByVal sURL As String, ByVal xhr As Object, ByVal html As HTMLDocument) As Boolean
With xhr
.Open "GET", sURL, False
.send
html.body.innerHTML = .responseText
End With
CheckValidURL = html.querySelectorAll("#wistia_video").Length > 0
End Function
也使用Instr作品
Option Explicit
Public Sub Test()
Dim urls(), i As Long, html As HTMLDocument, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
urls = Array("https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
For i = LBound(urls) To UBound(urls)
MsgBox CheckValidURL(urls(i), xhr)
Next
End Sub
Public Function CheckValidURL(ByVal sURL As String, ByVal xhr As Object) As Boolean
With xhr
.Open "GET", sURL, False
.send
CheckValidURL = InStr(.responseText, "html") > 0
End With
End Function
重写您的信息:
Option Explicit
Public Sub Test()
Dim urls(), i As Long, html As HTMLDocument, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
urls = Array("https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
For i = LBound(urls) To UBound(urls)
MsgBox CheckValidURL(urls(i), xhr)
Next
End Sub
Public Function CheckValidURL(ByVal sURL As String, ByVal xhr As Object) As Boolean
With xhr
.Open "GET", sURL, False
.send
CheckValidURL = UBound(Split(.responseText, "<script", , vbTextCompare)) > 0
End With
End Function
答案 1 :(得分:0)
oXMLHTTP.responseText
您可以使用
oXMLHTTP.Status = 200
这是xmlHttp的状态列表
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms767625(v%3Dvs.85)