VBA Excel下载页面完成 [英] VBA Excel Download webpage complete

查看:81
本文介绍了VBA Excel下载页面完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载完整的网页.换句话说,可以自动执行此过程:

I'm trying to download a complete webpage. In other words automate this process:

1-打开网页2-单击另存为3-选择完成4-关闭网页.

1- Open the webpage 2- Click on Save as 3- Select Complete 4- Close the webpage.

这是我到目前为止所得到的:

This is what I've got so far:

URL = "google.com" 'for TEST
Dim IE
Set IE = CreateObject("Internetexplorer.Application")
IE.Visible = False
IE.Navigate URL
Do
Loop While IE.Busy = True
Dim i
Dim Filename
i = 0
Filename = "C:\Test.htm"
IE.Document.ExecCommand "SaveAs", False, Filename 

当我在最后一行中运行代码时,将出现一个保存文件对话框.有什么办法可以抑制这种情况?

When I run the code in the last line a save file dialog appears. Is there any way to suppress this?

任何帮助将不胜感激.

推荐答案

另存为"对话框

The Save As dialog cannot be suppressed:

从脚本调用此方法时,无法取消保存HTML文档"对话框.

The Save HTML Document dialog cannot be suppressed when calling this method from script.

这也是一个模式对话框,您无法自动单击保存"按钮.面对此类对话框时,VBA执行会暂停,同时等待用户手动输入.

It is also a modal dialog and you cannot automate the way to click the "Save" button. VBA execution pauses while waiting manual user input when faced with a dialog of this sort.

您可以尝试使用标准的I/O功能读取页面的HTML并将其打印到文件中,而不是使用 IE.Document.ExecCommand 方法.

Rather than using the IE.Document.ExecCommand method, you could try to read the page's HTML and print that to a file using standard I/O functions.

Option Explicit
Sub SaveHTML()
Dim URL as String
Dim IE as Object
Dim i as Long
Dim FileName as String
Dim FF as Integer

URL = "http://google.com" 'for TEST
Filename = "C:\Test.htm"

Set IE = CreateObject("Internetexplorer.Application")
IE.Visible = True
IE.Navigate URL
Do
Loop While IE.Busy

'Creates a file as specified
' this will overwrite an existing file if already exists
CreateObject("Scripting.FileSystemObject").CreateTextFile FileName

FF = FreeFile
Open Filename For Output As #FF

With IE.Document.Body
    Print #FF, .OuterHtml & .InnerHtml
End With

Close #FF

IE.Quit
Set IE = Nothing
End Sub

我不确定这是否能满足您的需求.还有其他从网络获取数据的方法,最好的方法是从 XMLHTTP请求,并将其打印到文件中.

I am not sure whether this will give you exactly what you want, or not. There are other ways to get data from web and probably the best would be to get the raw HTML from an XMLHTTP request and print that to a file.

当然,很少需要我们实际需要HTML格式的整个网页,因此,如果您要从网页中抓取特定数据,那么XMLHTTP和DOM将是最好的方法,根本不需要将其保存到文件中.

Of course, it is rarely the case that we actually need an entire web page in HTML format, so if you are looking to then scrape particular data from a web page, the XMLHTTP and DOM would be the best way to do this, and it's not necessary to save this to a file at all.

或者,您可以使用Selenium包装器来自动化IE,这比对InternetExplorer.Application类使用相对较少的本机方法要强大得多.

Or, you could use the Selenium wrapper to automate IE, which is much more robust than using the relatively few native methods to the InternetExplorer.Application class.

还请注意,您正在使用一种相当粗糙的方法来等待网页加载( IE.Busy时循环).尽管有时 可以工作,但它可能并不可靠.关于如何在SO上正确执行此操作,存在许多问题,因此,我将向您介绍此处的搜索功能,以对该代码进行一些调整.

Note also that you are using a rather crude method of waiting for the web page to load (Loop While IE.Busy). While this may work sometimes, it may not be reliable. There are dozens of questions about how to do this properly here on SO, so I would refer you to the search feature here to tweak that code a little bit.

这篇关于VBA Excel下载页面完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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