2013-10-05 152 views
4

我使用Excel(因为数据保存在电子表格中)来检查几千个链接的列表并查看它们是否损坏,AKA是否返回404或者当我使用RESTful获取它们时。但是,我的VBA代码正在返回除curl之外的其他内容,并且在这种情况下,curl是正确的(该页面已存在)。这是我与引发问题的链接代码(这是一个子程序内,不用担心):当curl返回200时,VBA GET请求返回404

For i = StartRow To EndRow 
    Let copyRange = "H" & i 
    Let writeRange = "T" & i 
    query = Worksheets("Sheet1").Range(copyRange).Value 
    If query = "" Then 

    Else 
     With zipService 
      .Open "GET", query, False 
     End With 
     zipService.send 
     zipResult = zipService.Status 
     Worksheets("Sheet1").Range(writeRange).Value = zipResult 
    End If 

Next i 

curl命令行:

curl –sL –w “%{http_code} \n” http://URLHERE” –o /dev/null 

的联系是造成麻烦:http://www.stopwaste.org/home/index.asp#

Curl返回200,VBA返回404.相同的链接已经多次失败,所以我不认为这是“服务器刚刚停止一秒”的情况。我认为这是我的#字符问题,所以我删除了它,但得到了完全相同的响应。该代码正在成功报告其他404,所以不知何故,这种情况会产生误报(或否定性,无论您希望如何调用它)。 “如果查询=”“然后”,以便没有GET空URLS,VBA不喜欢那些。

我在这里很难过,希望有人能够帮助我。提前致谢!

回答

3

为什么不使用.FollowHyperlink来检查URL是否存在?

看到这个例子。

Sub Sample() 
    Dim url As String 

    url = "http://www.stopwaste.org/home/index.asp#" 
    Debug.Print url, CheckIfURLExists(url) 

    url = "http://www.Google.com" 
    Debug.Print url, CheckIfURLExists(url) 

    url = "http://www.Goo00000gle.com" 
    Debug.Print url, CheckIfURLExists(url) 
End Sub 

Function CheckIfURLExists(ByVal sLink As String) As Boolean 
    On Error Resume Next 
    ThisWorkbook.FollowHyperlink (sLink) 
    CheckIfURLExists = Err.Number = 0 
End Function 

截图:

enter image description here

+0

我与所有这些答案的组合去(双,三重检查),但你的是最有帮助的,因为它给了我一个替代方法。 – PunDefeated

0

尝试改变这条线,包括双引号:

zipService.send "" 

另外,尽量每个URL

Set zipService = Nothing 

我假设的Excel版本适用于后正在重置zipService自你指出一个具体的URL以来,你的一些URL。

1

删除了 '#' 字符和它的工作就好了。

enter image description here

在菲德勒我看到浏览器未发送“#”字符都:

enter image description here