Excel 检查Wistia的有效URL

Excel 检查Wistia的有效URL,excel,vba,web-scraping,xmlhttprequest,Excel,Vba,Web Scraping,Xmlhttprequest,我找到了一个代码,我将其转换为UDF,以检查wistia的url是否有效 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 aScript

我找到了一个代码,我将其转换为UDF,以检查wistia的url是否有效

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
子测试()
MsgBox CheckValidURL(“https://fast.wistia.net/embed/iframe/vud7ff4i6w")
端接头
函数CheckValidURL(sURL作为字符串)作为布尔值
Dim oXMLHTTP作为对象
Dim sResponseText作为字符串
作为变体的Dim属性部件
设置oXMLHTTP=CreateObject(“MSXML2.XMLHTTP”)
打开“GET”,sURL,False
发送
sResponseText=oXMLHTTP.responseText
aScriptParts=拆分(sResponseText,intead of

oXMLHTTP.responseText
你可以用

oXMLHTTP.Status = 200 
以下是xmlHttp的状态列表


您可以通过在sub中创建xhr对象并传递给函数来获得效率,然后只查看响应头
链接

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

备选方案:

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
在函数测试中,检查是否存在仅存在于有效链接中的id或字符串(按照您的方式)


也使用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
选项显式
公共子测试()
Dim URL(),i为长,html为HTMLDocument,xhr为对象
设置xhr=CreateObject(“MSXML2.XMLHTTP”)
URL=数组(“https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
对于i=LBound(URL)到UBound(URL)
MsgBox checkvalidull(URL(i),xhr)
下一个
端接头
公共函数CheckValidURL(ByVal sURL作为字符串,ByVal xhr作为对象)作为布尔值
使用xhr
.打开“GET”,sURL,False
.发送

CheckValidURL=UBound(拆分(.responseText,)由于您的代码中没有任何问题/错误,请尝试在代码审阅网站中发布此内容。感谢您的建议。事实上,我正在搜索其他解决方案。什么决定了此解决方案是否有效?如下图所示的200响应代码?请提供有效和无效的url。此url有效“”,您可以在url末尾添加一些字母以获取无效链接假设页面实现保持不变,您的方法没有问题。非常感谢。我尝试了两个链接,一个有效,一个无效,两个都返回True!!!在这种情况下,“invalid URL”仍然返回200,只有一条“not found”消息。很好,如果“not found”,您可以使用那么InvalidYou能告诉我“未找到”这一点是如何实现的吗?如果oXMLHTTP.Status=200和sResponseText“未找到”,那么CheckValidURL=true end如果对于xhr的第一个解决方案,我在测试
?.getResponseHeader(“内容编码”)时都得到了False
我得到的是
br
而不是
identity
我希望有效的是identity。你能粘贴.getAllResponseHeader的结果以指示哪个应该通过,哪个应该失败吗?另外,请尝试:CheckValidURL=Not.getResponseHeader(“链接”)=vbNullStringYes它现在正在使用“link”。非常感谢您的帮助,这样会更有效,因为您只需要处理标题