将网页保存为MHTM文件

时间:2009-06-15 10:12:44

标签: internet-explorer com powershell

给定一个特定的URL,我试图将该URL的内容下载为MHT文件。我考虑过编写解析器/爬虫,但认为必须有更快的方法。

我解雇了Powershell:

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://www.example.com")
$ie.document.title # Just to verify the navigate worked

此时我找不到调用菜单命令的方法,尤其是SaveAs。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

使用VBScript

对于本地文件

cscript yourscriptname.vbs file:/test.html test.mht

对于远程文件

cscript yourscriptname.vbs http://www.test.com/test.html test.mht

-

Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1
Const adTypeText = 2

Set args = WScript.Arguments

if args.Count = 0 then
WScript.Echo "Usage: [CScript | WScript] mht_converter.vbs <html file> <mht filename>"
WScript.Quit 1
end if

Set objMessage = CreateObject("CDO.Message")
objMessage.CreateMHTMLBody args.Item(0)
SaveToFile objMessage, args.Item(1)

Sub SaveToFile(Msg, Fn)
Dim Strm, Dsk
Set Strm = CreateObject("ADODB.Stream")
Strm.Type = adTypeText
Strm.Charset = "US-ASCII"
Strm.Open
Set Dsk = Msg.DataSource
Dsk.SaveToObject Strm, "_Stream"
Strm.SaveToFile Fn, adSaveCreateOverWrite
End Sub