【问题标题】:Is the implementation of response.info().getencoding() broken in urllib2?response.info().getencoding() 的实现在 urllib2 中被破坏了吗?
【发布时间】:2009-08-20 22:41:40
【问题描述】:

标签: python encoding urllib2


【解决方案1】:

嗯,你认为坏的是什么?

我获得了 urllib 和 wget 的 ISO-8859-2(我目前在波兰)。我用 Firefox 得到 UTF-8。这是因为我的 Firefox 告诉站点它接受 ISO-8859-1 和 UTF-8,而 wget 和 urllib2 什么也没说。相关的请求头是:

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

从中删除 UTF-8,您将不会获得 UTF-8,可通过远程登录到端口 80 轻松测试。

Google.com 简单地(并且合理地)默认为 ISO-8859-1,google.pl 为 ISO-8859-2,我相信其他网站还有其他默认值。

我没有得到 wget、urllib2 或 telnet 的编码标头,我猜 urllib2 然后假定为 7bit,这可能有点荒谬,因为 Content-Encoding 通常是 gzip 或什么都没有。

【讨论】:

    【解决方案2】:

    根据the document

    Message.getencoding()

    返回 Content-Transfer-Encoding 消息头中指定的编码。如果不存在这样的标头,则返回“7bit”。编码转换为小写。

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 2021-01-13
      • 2013-07-13
      • 2021-05-17
      相关资源
      最近更新 更多