【问题标题】:How to check for Ajax loading while scraping using VBA如何在使用 VBA 抓取时检查 Ajax 加载
【发布时间】:2011-10-11 13:18:43
【问题描述】:
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
  Close #nextFileNum

End Sub

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

这是我正在使用的代码,它适用于静态或非 ajax 网站,但在 ajax 的情况下,内容会丢失。

【问题讨论】:

  • 使用 MSXML2.XMLHTTP 你不会因为内容不会加载。看看是否有可用的 API 然后使用 XMLHTTP 或切换到 IE/Selenium

标签: vba screen-scraping


【解决方案1】:

如果问题是如何检查网页发出的 ajax 请求,您可以查看浏览器开发者工具区域的网络选项卡。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 2015-10-23
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多