2009-06-15 193 views
0

给定一个特定的URL,我试图将该URL的内容作为MHT文件下载。我认为写一个解析器/爬行器,但认为必须有一个更快的方法。将网页保存为MHTM文件

我解雇了PowerShell的:

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

在这一点上我无法找到一个方法来调用菜单命令,尤其另存为。

任何帮助,将不胜感激。

+0

正如你可以看到http://msdn.microsoft。 com/en-us/library/aa752084(VS.85).aspx无法调用SaveAs只有导航功能 – jitter 2009-06-15 11:15:03

回答

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