许多工具有办法导出.mht文件。 我想办法到单个文件转换为文件的集合,一个HTML文件,相关图片和CSS文件,我可以再上传到网站托管商,所有的浏览器是消耗品。 有谁知道任何工具或库或算法来做到这一点。
Answer 1:
那么,你可以在IE中打开和保存.mht文件为AA网页。 我这个页面进行了测试,即使它在IE看上去很奇怪(这是IE毕竟),它保存,然后在Chrome中打开的细(如,它看起来像它应该)。
除非该方法,看文件本身,文本块保存在文件中的原样,并且所有其他内容保存为Base64。 内容中的每一项前面有:
[Boundary]
Content-Type: [Mime Type]
Content-Transfer-Encoding: [Encoding Type]
Content-Location: [Full path of content]
其中[Mime类型],[ 编码类型],[ 内容的完整路径]是可变的。 [编码类型]似乎是要么BASE64或引号的可打印 。 [边界]在像这样的.MHT文件的开头定义的:
From:
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"
利用这一点,如果需要的话你可以把自己的文件分析器。
Answer 2:
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
Answer 3:
除了IE和MS Word,有一个名为“mht2html”写入一个开源的跨平台计划2007年并在最后更新2016 。 它既有一个GUI和终端接口。
官方网站
SourceForge项目
我没有测试它尚未,但它似乎已经收到很好的评价。
Answer 4:
我认为,@ XGundam05是正确的。 下面是我做的,使其工作。
我开始在Visual Studio中的Windows窗体项目。 增加了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");
}
您应该能够借此代码并添加文件的列表,并处理每一个用foreach 。 所述webBrowser包含一个称为方法ShowSaveAsDialog() ; 这将允许将其保存为.mht文件或只是HTML或完整的页面。
编辑:您可以使用Web浏览器的文件和刮在这一点上的信息。 通过在此处添加一个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
}
}
}
}
Answer 5:
因此,自动化IE是困难的,而不是使用端到端的,所以我认为建立某种形式的代码,不会这将是要走的路。 在github上,我发现这条巨蟒其中一个可能是好的
https://github.com/Modified/MHTifier http://decodecode.net/elitist/2013/01/mhtifier/
如果我有时间,我会努力做好在PowerShell中类似的事情。
Answer 6:
Firefox拥有嵌入式工具。 进入菜单(按Alt如果隐藏) File->Convert saved pages 。
Answer 7:
第1步:打开.MHT / .MHTML文件在浏览器中。
第2步:右键点击选择要查看源代码。
第3步:复制源代码并将其粘贴到一个新的.TXT文件,然后更改文件扩展名.HTML。
文章来源: How can you programmatically (or with a tool) convert .MHT mhtml files to regular HTML and CSS files?