简体   繁体   中英

Saving webpage as MHTM file

Given a specific URL, I'm trying to download the contents of that URL as an MHT file. I considered writing a parser/crawler, but think there must be a quicker way.

I fired up Powershell:

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

AT this point I couldn't find a way to call the menu commands, particularly SaveAs.

Any help would be appreciated.

With VBScript

For local files

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

For remote files

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM