VBA从网站下载文件-弹出窗口 [英] VBA download file from website - popup window

查看:94
本文介绍了VBA从网站下载文件-弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动从网站下载文件.当我手动进行下载时,我所要做的就是单击保存"图标(软盘),然后另一个窗口弹出一秒钟,然后开始下载(弹出的窗口消失了).

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).

我通常要做的(当我自动进行下载时)是找到文件的URL,然后使用 URLDownloadToFile 函数.但是在这种情况下,我无法在html中找到该url.我试图在对象上使用 .click FireEvent ,但没有任何效果.

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.

因此,我开始认为(基于本站点的类似问题),当我按下保存"图标时,脚本会生成URL.不幸的是,我不熟悉javascript或它如何工作.现在,我正在尝试使用浏览器的开发人员工具的控制台来确定单击对象时发生的情况.顺便说一句:此对象是< img> 对象.

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.

我在网上搜索了答案,我认为如果要下载文件,则必须使用 execScript 之类的方法自己调用javascript.但是,当我单击该图标时,如何找出调用哪个脚本?更重要的是,如果我不完全了解网页的工作原理,我是否能够理解您的答案?:)

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 .:我知道,如果可以给您网站地址,会容易得多,但是需要登录才能查看我所谈论的内容...

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...

推荐答案

有帮助吗?

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

这篇关于VBA从网站下载文件-弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆