VBA Excel 下载网页完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27419804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:30:03  来源:igfitidea点击:

VBA Excel Download webpage complete

excelvba

提问by Pedrumj

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

我正在尝试下载一个完整的网页。换句话说,自动化这个过程:

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

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

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?

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

Any help would be most appreciated.

非常感激任何的帮助。

回答by David Zemens

The Save As dialog cannotbe suppressed:

“另存为”对话框不能被抑制

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

从脚本调用此方法时,无法抑制“保存 HTML 文档”对话框。

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.

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

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

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

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

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 requestand print that to a file.

我不确定这是否会给你你想要的,或者不。还有其他方法可以从 Web 获取数据,最好的方法可能是从XMLHTTP 请求中获取原始 HTML并将其打印到文件中。

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.

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

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.

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

Note also that you are using a rather crude method of waiting for the web page to load (Loop While IE.Busy). While this maywork 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.

另请注意,您正在使用一种相当粗糙的方法来等待网页加载 ( Loop While IE.Busy)。虽然这有时可能有效,但它可能不可靠。这里有很多关于如何在 SO 上正确执行此操作的问题,因此我建议您使用此处的搜索功能来稍微调整该代码。