问题
I am trying to automate a file downloading from a website. When I do the download manually, all I have to do is to click on the "save" icon (floppy disk), then another window pops up for a second and the download begins (while the popped up window disappears).
What I usually do (when I automate a download) is to find the files URL, then I use the URLDownloadToFile
function. But in this case I cannot find the url in the html. I tried to use the .click
and FireEvent
on the object but nothing worked.
So I started to think (based on similar question in this site) that a script generates the URL when I press the "save" icon. Unfortunately I am not familiar with javascript or how it works. Right now I am trying to use my browser's developer tools's console to figure out what happens when I click the object. BTW: this object is an <img>
object.
I searched the web for answers, and I think that somehow I will have to call the javascript myself if I want to download the file, with something like execScript
. But how do I find out which script gets called when I click on the icon? And more importantly will I be able to understand your answer without completely understanding how a webpage works? :)
P.S.: I know it would be far easier if I could give you the site address, but it requires a login to see the stuff that I am talking about...
回答1:
Assuming you are trying to download the image -
Have a look at How do I base64 encode a string efficiently using Excel VBA?
There you'll find a Decode method. Use that on the base64 portion of the url, that is the text between "data:image/png;base64,
and "
. Save that as a binary file and you have your image.
回答2:
Does this help?
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Sub DownloadFilefromWeb()
Dim strSavePath As String
Dim URL As String, ext As String
Dim buf, ret As Long
URL = Worksheets("Sheet1").Range("A2").Value
buf = Split(URL, ".")
ext = buf(UBound(buf))
strSavePath = "C:\Users\rshuell\Desktop\Downloads\" & "DownloadedFile." & ext
ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
If ret = 0 Then
MsgBox "Download has been succeed!"
Else
MsgBox "Error"
End If
End Sub
Or....
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub pMain()
Dim sURL As String
Dim sDestination As String
Dim bSuccess As Boolean
Dim lRow As Long
Dim ws As Excel.Worksheet
Dim strSavePath As String
Dim URL As String, ext As String
Dim buf, ret As Long
'Change to suit
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
For lRow = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row
sURL = .Cells(lRow, "A")
sDestination = .Cells(lRow, "B")
buf = Split(sURL, ".")
ext = buf(UBound(buf))
pos = InStrRev(sURL, "/", -1)
file = Mid(sURL, pos + 1, 99)
strSavePath = sDestination & file
ret = URLDownloadToFile(0, sURL, strSavePath, 0, 0)
If ret = 0 Then
.Cells(lRow, "C") = "File download successfully!"
Else
.Cells(lRow, "C") = "Couldn't download the file!"
End If
DoEvents
Next lRow
End With
End Sub
来源:https://stackoverflow.com/questions/50193629/vba-download-file-from-website-popup-window