我正在尝试自动从网站下载文件。当我手动下载时,我所要做的就是点击“保存”图标(软盘),然后弹出另一个窗口一秒钟,下载就开始了(弹出的窗口消失了)。
我通常所做的(当我自动下载时)是找到文件的URL,然后我使用URLDownloadToFile函数。但是在这种情况下,我在html中找不到url。我尝试在对象上使用.click和FireEvent,但都不起作用。
因此,我开始思考(基于这个站点中的类似问题)当我按下“保存”图标时,脚本会生成URL。不幸的是,我不熟悉javascript,也不熟悉它是如何工作的。现在,我正在尝试使用浏览器的开发人员工具的控制台来弄清楚当我单击对象时会发生什么。顺便说一句:这个对象是一个<img>对象。
我在网上寻找答案,我想如果我想用execScript这样的东西下载文件,我必须自己调用javascript。但是,当我单击图标时,如何找出调用哪个脚本呢?更重要的是,我是否能够在不完全理解网页工作原理的情况下理解您的答案?:)
附言:我知道如果我能给你网站地址会容易得多,但它需要登录才能看到我正在谈论的东西……
发布于 2018-05-06 21:45:08
这有帮助吗?
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或者...
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
复制相似问题