Excel VBA script to find 404 errors in a list of URLs?

前端 未结 2 1043
感情败类
感情败类 2020-12-10 00:20

So, I have this spreadsheet with a list of about 5000 URLs. (All pages on our corporate intranet.)

We know some of the links are broken, but don\'t know of a good w

相关标签:
2条回答
  • 2020-12-10 00:29

    Here is an example to check the status line from a list of URL with Excel:

    Sub TestLinks()
      Dim source As Range, req As Object, url$
      Set req = CreateObject("Msxml2.ServerXMLHTTP.6.0")
    
      ' define were the links and results are
      Set source = Range("A1:B2")
    
      ' clear the results
      source.Columns(2).Clear
    
      ' iterate each row
      For i = 1 To source.Rows.count
        ' get the link from the first column
        url = source.Cells(i, 1)
    
        ' send the request using a HEAD to check the status line
        req.Open "HEAD", url, False
        req.setRequestHeader "Accept", "image/webp,image/*,*/*;q=0.8"
        req.setRequestHeader "Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6"
        req.setRequestHeader "Accept-Encoding", "gzip, deflate"
        req.setRequestHeader "Cache-Control", "no-cache"
        req.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
        req.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"
        req.Send
    
        ' write the result in the second column
        source.Cells(i, 2) = req.Status
      Next
    
      MsgBox "Finished!"
    End Sub
    
    0 讨论(0)
  • 2020-12-10 00:51

    Use a user defined function to return HTML-Status Codes and drag it down next to the links. Might take a while for Excel to check 5000 links, though.

    Public Function CheckURL(url As String) As String
    Dim request As New WinHttpRequest
    request.Open "GET", url
    request.Send
    CheckURL = request.Status
    End Function
    

    You will probably need to add a reference to "Microsoft WinHTTP Services" under "Extras" -> "References"

    0 讨论(0)
提交回复
热议问题