代码之家  ›  专栏  ›  技术社区  ›  Saqib

将网页另存为MHTM文件

  •  0
  • Saqib  · 技术社区  · 17 年前

    给定一个特定的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 回复  |  直到 17 年前
        1
  •  2
  •   jitter    17 年前

    使用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
    
    推荐文章