linux - html2文本 : convert special characters

标签 linux command-line

所以首先,我不是在谈论 python html2text,而是命令行工具:http://www.mbayer.de/html2text/

我在我的程序中使用这个工具来为 html 文章生成一些小的文本预览。 到目前为止,这是我使用的行:

html2text -utf8 -nobs -style pretty filename.html

不幸的是,我现在遇到了使用特殊字符的 html 代码,例如

ä

那些不会像预期的那样转换为“ä”甚至“ae”。

例如行:

<p class="caption">Steve Ballmer, Jahrgang 56, wird zumindest auf diesem Bild auf 56 Jahre gesch&#xE4;tzt. </p>

应转换为:

Steve Ballmer, Jahrgang 56, wird zumindest auf diesem Bild auf 56 Jahre geschätzt.

而是在此处生成此行:

Steve Ballmer, Jahrgang 56, wird zumindest auf diesem Bild auf 56 Jahre gesch&#xE4;tzt.

文档说:

By default, when -nometa is supplied, html2text uses ISO 8859-1 for the input. Specifying this option, UTF-8 is used instead (both for input and output).

所以我尝试省略“-utf8”并使用“-nometa”。但仍然是相同的结果:( 我错过了一个选项吗?

提前感谢您的帮助。

最佳答案

你试过 (html) tidy 了吗?

tidy filename.html | html2text -utf8 -nobs -style pretty

可能包括抑制错误输出:

tidy filename.html 2>/dev/null | html2text -utf8 -nobs -style pretty

至少你例子中的简单段落完成了

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
<title></title>
</head>
<body>
<p class="caption">Steve Ballmer, Jahrgang 56, wird zumindest auf
diesem Bild auf 56 Jahre gesch&auml;tzt.</p>
</body>
</html>

关于linux - html2文本 : convert special characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30015809/

相关文章:

PHP 脚本作为 Debian 中的守护进程

c - 如何在linux中获得高精度纳秒级延迟

python - 如何在 Debug模式下将命令行参数从 VS 传递给 Python?

c - 如何用来自/dev/urandom 的 100 MB 数据填充文件?

linux - SWT 浏览器和 Eclipse

linux - md5sum linux 命令的哈希长度

Linux 用户添加到目录

windows - 传递包含连字符的命令行参数

Linux 上的 Swift 工具链位置

mysql - 对带重定向和不带重定向的 `mysql -e` 输出格式有点好奇