所以首先,我不是在谈论 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ä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ä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ätzt.</p>
</body>
</html>
关于linux - html2文本 : convert special characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30015809/