2013-04-24 35 views

回答

10

那麼,你可以在IE中打開.MHT文件,並將其另存爲一個網頁。我測試了這個頁面,儘管它在IE瀏覽器中看起來很奇怪(畢竟是IE),但它保存並在Chrome中正常打開(因爲它看起來應該是這樣)。

禁止該方法,查看文件本身,將文本塊保存原樣,並將所有其他內容保存在Base64中。各內容項前面有:

[Boundary] 
Content-Type: [Mime Type] 
Content-Transfer-Encoding: [Encoding Type] 
Content-Location: [Full path of content] 

[Mime類型][編碼類型],和[內容完整路徑]是可變的。 [編碼類型]似乎是base64引用可打印[邊界]在.mht文件的開頭定義,像這樣:

From: <Saved by WebKit> 
Subject: converter - How can you programmatically (or with a tool) convert .MHT mhtml  files to regular HTML and CSS files? - Stack Overflow 
Date: Fri, 9 May 2013 13:53:36 -0400 
MIME-Version: 1.0 
Content-Type: multipart/related; 
    type="text/html"; 
    boundary="----=_NextPart_000_0C08_58653ABB.B67612B7" 

使用,如果需要的話,你可以把自己的文件分析器。

+0

所以IE會創建一個文件夾並分別保存圖像等?我不知道你是否可以自動化IE來做到這一點與COM對象? – klumsy 2013-05-09 22:06:38

+0

是的,IE創建一個文件夾與所有的圖像和whatnot。 COM對象顯示了一個'Navigate2'函數和事件處理程序(用於完成等),但是我在其引用中找不到保存函數。這並不意味着它不在那裏,只是我無法找到它。 – XGundam05 2013-05-10 13:06:21

+0

我玩這個更多,我可以加載它,並保存hack自動鍵盤的另存爲對話框,這是hacky和脆弱的。然而它希望將它保存爲MHT而不是完整的HTML(作爲一個完整的網站在線保存網站工作正常),並且我找不到使用ExecWB作爲選項進行保存的具體方法,因此最好的做法可能是隻是用代碼來處理MHT,或者嘗試用其他的自動化方式來處理硒,或者使用forefox或chrome自動化或擴展等。 – klumsy 2013-05-15 06:17:18

0

我認爲@ XGundam05是正確的。這是我做的工作。

我從Visual Studio中的Windows Form項目開始。將WebBrowser添加到表單中,然後添加兩個按鈕。然後,將此代碼:

private void button1_Click(object sender, EventArgs e) 
    { 
     webBrowser1.ShowSaveAsDialog(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     webBrowser1.Url = new Uri("localfile.mht"); 
    } 

您應該能夠藉此代碼並添加文件的列表,並處理每一個有foreachwebBrowser包含一種稱爲ShowSaveAsDialog()的方法;這將允許一個保存爲.mht或只是html或整個頁面。

編輯:你可以使用webBrowser的文檔,並在這一點上刮信息。通過添加一個RichTextBox和公共變量按照MS的位置:http://msdn.microsoft.com/en-us/library/ms171713.aspx

public string Code 
    { 
     get 
     { 
      if (richTextBox1.Text != null) 
      { 
       return (richTextBox1.Text); 
      } 
      else 
      { 
       return (""); 
      } 
     } 
     set 
     { 
      richTextBox1.Text = value; 
     } 
    } 


    private void button2_Click(object sender, EventArgs e) 
    { 
     webBrowser1.Url = new Uri("localfile.mht"); 
     HtmlElement elem; 

     if (webBrowser1.Document != null) 
     { 

      HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("HTML"); 
      if (elems.Count == 1) 
      { 
       elem = elems[0]; 
       Code = elem.OuterHtml; 
       foreach (HtmlElement elem1 in elems) 
       { 
        //look for pictures to save 
       } 

      } 
     } 
    } 
+0

每個你的傢伙解決方案和這個http://stackoverflow.com/questions/872750/savinga-a-它看起來似乎是不可能的,如果沒有彈出saveas對話框,這似乎是不可能的。我希望能夠自動化這個enmasse – klumsy 2013-05-14 22:17:33

+0

隨着編輯,你可能會想出一個過程來刮和保存的HTML和圖像。 – CaptainBli 2013-05-14 23:35:38

1

除了IE和MS Word,還有這個所謂的 'mht2html' 在SourceForge.net上開源的跨平臺的程序:

http://sourceforge.net/projects/mht2htm/

我還沒有測試過,但它似乎已收到良好的評論。

P.S.對不起,提供了這樣一個老問題的答案。

0

MHT文件本質上是MIME。因此,可以使用Chilkat.Mime或完全免費的System.Net.Mime組件來訪問其內部結構。例如,如果MHT包含圖像,則可以用輸出HTML中的base64字符串替換它們。

Imports HtmlAgilityPack 
Imports Fizzler.Systems.HtmlAgilityPack 
Imports Chilkat 
Public Function ConvertMhtToHtml(ByVal mhtFile As String) As String 
    Dim chilkatWholeMime As New Chilkat.Mime 
    'Load mime' 
    chilkatWholeMime.LoadMimeFile(mhtFile) 
    'Get html string, which is 1-st part of mime' 
    Dim html As String = chilkatWholeMime.GetPart(0).GetBodyDecoded 
    'Create collection for storing url of images and theirs base64 representations' 
    Dim allImages As New Specialized.NameValueCollection 
    'Iterate through mime parts' 
    For i = 1 To chilkatWholeMime.NumParts - 1 
     Dim m As Chilkat.Mime = chilkatWholeMime.GetPart(i) 
     'See if it is image' 
     If m.IsImage AndAlso m.Encoding = "base64" Then 
      allImages.Add(m.GetHeaderField("Content-Location"), "data:" + m.ContentType + ";base64," + m.GetBodyEncoded) 
     End If : m.Dispose() 
    Next : chilkatWholeMime.Dispose() 
    'Now it is time to replace the source attribute of all images in HTML with dataURI' 
    Dim htmlDoc As New HtmlDocument : htmlDoc.LoadHtml(html) : Dim docNode As HtmlNode = htmlDoc.DocumentNode 
    For i = 0 To allImages.Count - 1 
     'Select all images, whose src attribute is equal to saved URL' 
     Dim keyURL As String = allImages.GetKey(i) 'Saved url from MHT' 
     Dim elementsWithPics() As HtmlNode = docNode.QuerySelectorAll("img[src='" + keyURL + "']").ToArray 
     Dim imgsrc As String = allImages.GetValues(i)(0) 'dataURI as base64 string' 
     For j = 0 To elementsWithPics.Length - 1 
      elementsWithPics(j).SetAttributeValue("src", imgsrc) 
     Next 
     'Select all elements, whose style attribute contains saved URL' 
     elementsWithPics = docNode.QuerySelectorAll("[style~='" + keyURL + "']").ToArray 
     For j = 0 To elementsWithPics.Length - 1 
      'Get and modify style' 
      Dim modStyle As String = Strings.Replace(elementsWithPics(j).GetAttributeValue("style", String.Empty), keyURL, imgsrc, 1, 1, 1) 
      elementsWithPics(j).SetAttributeValue("style", modStyle) 
     Next : Erase elementsWithPics 
    Next 
    'Get final html' 
    Dim tw As New StringWriter() 
    htmlDoc.Save(tw) : html = tw.ToString : tw.Close() : tw.Dispose() 
    Return html 
End Function 
+0

這是什麼編程語言? – 2017-11-03 14:20:39

+1

這是VB.Net。它使用開源軟件包「Fizzler.Systems.HtmlAgilityPack」和商業軟件包「Chilkat.Mime」。但是奇爾卡特可以被「System.Net.Mime」類取代。 – Zagavarr 2017-11-13 12:37:06

-1

火狐具有嵌入式工具。轉到菜單(如果隱藏,請按Alt鍵)File->Convert saved pages

-1

第1步:在瀏覽器中打開.MHT/.MHTML文件。

第2步:右鍵點擊選擇查看源代碼。

第3步:複製源代碼並將其粘貼到新的.TXT文件,然後將文件擴展名更改爲.HTML。

相關問題