0
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的情況下內容丟失。如何檢查使用VBA報廢時的Ajax加載