【问题标题】:What's wrong with this html page?这个html页面有什么问题?
【发布时间】:2016-04-13 17:50:40
【问题描述】:

我想将特定页面转换为文本文件或使用 wget(例如-m)下载其中的一些链接,但是大多数工具都无法解释源代码。

例如:

$ curl http://www.free-energy-info.co.uk/ | html2text
curl: (23) Failed writing body (0 != 2896)

但是使用-check 会正确返回源代码:

$ html2text -check http://www.free-energy-info.co.uk/
<HTML>
<HEAD>
<TITLE>
...

然后我发现这个页面包含一些二进制数据和特殊字符,例如:

$ curl -s http://www.free-energy-info.co.uk/ | head | cat -v
M-^?M-~<^@h^@t^@m^@l^@>^@
^@
^@<^@h^@e^@a^@d^@>^@
^@
^@<^@T^@I^@T^@L^@E^@>^@F^@r^@e^@e^@-^@E^@n^@e^@r^@g^@y^@ ^@D^@e^@v^@i^@c^@e^@s^@,^@ ^@z^@e^@r^@o^@-^@p^@o^@i^@n^@t^@ ^@e^@n^@e^@r^@g^@y^@,^@ ^@a^@n^@d^@ ^@w^@a^@t^@e^@r^@ ^@a^@s^@ ^@H^@H^@O^@ ^@f^@u^@e^@l^@<^@/^@T^@I^@T^@L^@E^@>^@

这个网站有什么问题?是网络服务器的错误,源代码被故意打乱还是使用了一些特殊的格式(因为它看起来没有压缩)?如何使这个页面对于标准的 utils(如wget)可以理解?

请注意,它在网络浏览器中运行良好。

【问题讨论】:

  • 使用 curl 获得相同的结果,在发送有效的用户代理时也是如此。但是,wget 对我有用。
  • @Paul Wget 下载单个页面,但它没有跟随任何链接(指定 -m-r 时),就像它不理解源代码一样。

标签: html curl special-characters response wget


【解决方案1】:

页面只是UTF-16LE格式(可以通过W3 Validator查看)。

初始二进制序列(U+FEFF 用于 UTF-16BE,U+FFFE 用于 UTF-16LE)称为the byte order mark (BOM),它向程序发出信号表明文本是 Unicode 编码的(8 位、16位或 32 位)。

$ curl -s http://www.free-energy-info.co.uk/ | hexdump -n2
0000000 ff fe

所以听起来wgethtml2text 还不支持这种格式。它会下载,但问题在于内容解析(递归下载)。当前版本的 wget 假定输入数据可用于传统的 C 字符串函数,但不能使用 UTF-16 (Unicode)(请参阅:The input byte stream)。


至于解决方法,您可以使用iconv 命令将源代码转换回UTF-8,例如

$ curl -s http://www.free-energy-info.co.uk/ | iconv -f "UTF-16" -t "UTF-8" | head | cat -v
<html>
<head>
<TITLE>Free-Energy Devices, zero-point energy, and water as HHO fuel</TITLE>
<META NAME="Description" CONTENT="magnet power, free energy devices, power from aerials, gravity power, water power, renewable energy and electronics tutorial.">

因此,将带有curl 的页面保存为 UTF-8 的语法可能是:

curl -s http://example.com/ | iconv -f "UTF-16" -t "UTF-8" > index.html

然后让html2text工作,从标准输入读取源代码,例如

iconv -f "UTF-16" -t "UTF-8" <(curl -s http://www.free-energy-info.co.uk/) | html2text

对于 wget,您可以指定 --remote-encoding,但目前尚不支持,并且失败并显示:

此版本不支持 IRI

在 GNU Wget 1.16.3 中测试。


我已经在#47689 报告了这个错误,Tim Ruehsen 表示他已将 UTF-16 编码的 HTML 页面的支持解析提交给 wget 的下一个版本 2,但它仍然需要一些工作(例如 @987654341 @ 仍然不起作用)。

测试新版本后,尝试:

wget -r --local-encoding=UTF-16LE --input-file index.html --force-html --base http://www.free-energy-info.co.uk

按照Tim 的建议。

注意:如果您的 wget 编译时支持调试,请添加 -d

【讨论】:

    猜你喜欢
    • 2018-08-08
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多