开发者

How to check for Ajax loading while scraping using VBA

开发者 https://www.devze.com 2023-04-12 00:41 出处:网络
Private Sub CommandButton1_Click() Dim webpage As String webpage = GetWebpage(\"http://www.oddsportal.com/soccer/germany/bundesliga-2011-2012/b-moenchengladbach-bayer-leverkusen-806581/\")
Private Sub CommandButton1_Click()

Dim webpage As String

webpage = GetWebpage("http://www.oddsportal.com/soccer/germany/bundesliga-2011-2012/b-moenchengladbach-bayer-leverkusen-806581/")

Debug.Print webpage
Sheet1.Cells(12, 1) = webpage
End Sub

Function GetWebpage(url As String, Optional fileName As String) As String

Dim xml As Object ' MSXML2.XMLHTTP
Dim result As String

  Set xml = GetMSXML

  ' grab webpage
 With xml
    .Open "GET", url, True
    .send
  End With

  GetWebpage = xml.responseText

  ' write to file?
 If Len(fileName) > 0 Then
    If Not FileExists(fileName) Then
      Call CreateFile(fileName, GetWebpage)
    Else ' file exists
     If MsgBox("File already exists, overwrite?", vbYesNo) = vbYes Then
        Call CreateFile(fileName, GetWebpage)
      End If
    End If
  End If

End Function

Function GetMSXML() As Object
  On Error Resume Next
  Set GetMSXML = CreateObject("MSXML2.XMLHTTP.6.0")
End Function

Sub CreateFile(fileName As String, contents As String)
' create file from string contents

Dim tempFile As String
Dim nextFileNum As Long

  nextFileNum = FreeFile

  tempFile = fileName

  Open tempFile For Output As #nextFileNum
  Print #nextFileNum, contents
  Cl开发者_运维百科ose #nextFileNum

End Sub

Function FileExists(fileName As String) As Boolean
  FileExists = (Len(Dir(fileName)) > 0)
End Function

This the code I am using, it works well for static or non ajax sites, but in case of ajax the content is missing.


If the question is how to check what ajax requests a web page is making, you can look in the network tab of your browser's developer tools area.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号