0

我正在尝试获取并解析包含非 ASCII 字符的网页(网址为http://www.one.co.il)。这就是我所拥有的:

url = "http://www.one.co.il"
req = urllib2.Request(url)
response = urllib2.urlopen(req)
encoding = response.headers.getparam('charset') # windows-1255
html = response.read() # The length of this is valid - about 31000-32000,
                       # but printing the first characters shows garbage -
                       # '\x1f\x8b\x08\x00\x00\x00\x00\x00', instead of
                       # '<!DOCTYPE'
html_decoded = html.decode(encoding)

最后一行给了我一个例外:

File "C:/Users/....\WebGetter.py", line 16, in get_page
  html_decoded = html.decode(encoding)
File "C:\Python27\lib\encodings\cp1255.py", line 15, in decode
  return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0xdb in position 14: character maps to <undefined>

我尝试查看其他相关问题,例如urllib2 read to UnicodeHow to handle response encoding from urllib.request.urlopen(),但没有发现任何有用的信息。

有人可以在这个主题上阐明并指导我吗?谢谢!

4

1 回答 1

1

0x1f 0x8b 0x08 是 gzip 压缩文件的幻数。您需要先将其解压缩,然后才能使用其中的内容。

于 2017-08-23T17:11:43.717 回答