如何使用编程语言将 HTML 文件保存为非格式化文本,而不是代码?

How do I save HTML files as non-formatted text, not code, using a programming language?

我知道这个问题有点笼统,但我正在扩展我的 Windows 10 命令提示符,允许您在程序中以纯文本形式查看 HTML。我不知道不为这样的东西构建解释器是否会被认为是懒惰的,但对于只有我会使用的东西来说,它似乎工作太多了。为我知之甚少的标记语言制作解释器似乎没有必要,而在 Batch 中制作它会更难。

我知道如何读取文件并将它们存储为变量,但我的问题是如何将原始 HTML 存储为不带任何格式的纯文本。例如,

<p>Here's some text.</p>

会变成:

Here's some text.

我想要翻译人员将 HTML 转换为纯文本。不一定非得用Batch写,写了也没关系。不过,我更希望将它编写成一种更发达的语言,例如 Python,我以前曾见过它用于解释编程语言。不用自己写,推荐一个就好了。

对不起,如果我花时间解释。即使是部分解决方案也可以。感谢您的帮助!

PHP 有 strip_tags,也可以从命令行 运行。

$url = 'http://www.somedoma.in/some_file.htm';
$website =  file_get_contents($url);
echo strip_tags($website);

然后你可以使用php.exe到运行脚本。

Set ie = CreateObject("InternetExplorer.Application") 
ie.Visible = 0
ie.Silent = 1 
ie.Navigate2 "file://" & FilterPath & "Filter.html"

Do 
    wscript.sleep 50            
Loop Until ie.document.readystate = "complete"

ie.document.body.innerhtml = Inp.readall
Outp.write ie.document.body.innertext

'ie.quit

InP.ReadAll 是文本流对象中的 html 文本,OutP 包含另一个文本流对象中的纯文本。

导航到本地文件以删除安全对话框和限制。将该文件的文本(仅在内存中)替换为您的 html 文本 (ie.document.body.innerhtml = Inp.readall)。然后回读并写成文本 (Outp.write ie.document.body.innertext).

InP 和 OutP 在上面的代码片段中没有定义,但它们是文本流对象。

以后,请展示一些代码来证明您已尝试自行解决问题。类似 "Here are my requirements. Now write it for me or find me a tool" 的问题在这里通常不受欢迎。

但部分是为了避免进一步的一半答案,部分是因为我乐于接受挑战,这里有一个以混合 Batch + JScript 脚本编写的解决方案,它将把你的 HTML 的 innerText 写入安慰。使用 .bat 扩展名保存它。如果您希望输出转到文件而不是 batscript.bat htmlfile > outfile.txt 在 cmd 行。

@if (@CodeSection == @Batch) @then
@echo off & setlocal

if "%~1"=="" goto usage
if not exist "%~1" goto usage

cscript /nologo /e:JScript "%~f0" "%outfile%" < "%~1"
goto :EOF

:usage
2>&1 echo Usage: %~nx0 htmlfile
goto :EOF

@end // end Batch / begin JScript

var htmlfile = WSH.CreateObject('htmlfile');

htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
htmlfile.write(WSH.StdIn.ReadAll());

WSH.Echo(htmlfile.documentElement.innerText);
htmlfile.close();

IE9 兼容模式被调用以识别比没有时更多的 HTML 元素类型,同时仍然允许 Vista 兼容性。如果需要,您可以将 IE=9 更改为 10、11 或 Edge。


如果您更喜欢非混合脚本,您还可以使用 PowerShell 构造 htmlfile COM 对象。它的执行速度较慢,但​​代码更简单(尽管有奇怪的 .NET-ish 方法名称)。示例:

.bat 脚本:

@echo off & setlocal

if "%~1"=="" goto usage
if not exist "%~1" goto usage

set "htmlfile=%~f1"

set "psCommand="^
    $h=new-object -COM htmlfile;^
    $h.IHTMLDocument2_write('^<meta http-equiv="x-ua-compatible" content="IE=9" /^>');^
    $h.IHTMLDocument2_write(${%htmlfile%});^
    $h.documentElement.innerText""

powershell -noprofile -noninteractive %psCommand%

goto :EOF

:usage
echo Usage: %~nx0 htmlfile
goto :EOF

.ps1 脚本:

param( $htmlfile = $false )

if (-not (test-path $htmlfile)) {
    [console]::Error.WriteLine("Usage: $($MyInvocation.MyCommand.Name) htmlfile")
    exit
}

$html = gc $htmlfile | out-string
$hObj = new-object -COM htmlfile
$hObj.IHTMLDocument2_write('<meta http-equiv="x-ua-compatible" content="IE=9" />')
$hObj.IHTMLDocument2_write($html)
$hObj.documentElement.innerText
$hObj.Close()

(.ps1 解决方案的用法示例:powershell .\scriptname.ps1 htmlfile.html


因为我这样做是为了个人挑战,所以这里有一个批处理 + HTA 混合变体,可以将未保存的 innerText 粘贴到新的记事本 window 中,因为我可以。

<!-- : batch portion
@echo off & setlocal

if "%~1"=="" goto usage
if not exist "%~1" goto usage

mshta "%~f0" < "%~1"
goto :EOF

:usage
2>&1 echo Usage: %~nx0 htmlfile
goto :EOF

end Batch / begin HTA -->

<meta http-equiv="x-ua-compatible" content="IE=9" />
<div id="out"></div>

<script>
var fso = new ActiveXObject('Scripting.FileSystemObject'),
    osh = new ActiveXObject('WScript.Shell'),
    notepad = osh.Exec('notepad');

document.getElementById('out').innerHTML = fso.GetStandardStream(0).ReadAll();
clipboardData.setData('text', document.getElementById('out').innerText);

var waitActive = setInterval(function() {
    if (osh.AppActivate(notepad.ProcessID)) {
        clearInterval(waitActive);
        close(osh.SendKeys('^v'));
    }
}, 25);

</script>

我使用 HTA 来绕过浏览器安全机制以防止对剪贴板的写入访问(就像 htmlfile COM 对象一样),并且因为 HTA 重量更轻并且不太可能最终成为不可见的 运行 进程比 InternetExplorer.Application COM 对象。