首頁  >  問答  >  主體

编码 - Python 3.6中 'utf-8' codec can't decode byte invalid start byte?

Python 3.6中,网页信息解析失败,试了很多种编码,查看网页的编码方式也是utf-8。
错误信息:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte?
还有就是第一个print终端里打印出来的unicode内容是[b'\x1f\x8b\x08\x00\x...]这种格式的,之前也有过这种情况,一个print打2个变量,就是b'\x, 如果分来2行打又变回了汉字。是因为什么原因呢?

# -*- coding: utf-8 -*-
import json , sqlite3
import urllib.request

url = ('http://wthrcdn.etouch.cn/weather_mini?city=%E4%B8%8A%E6%B5%B7')
resp = urllib.request.urlopen(url)
content = resp.read()

print(content)
print(type(content))
print(content.decode('utf-8'))
PHP中文网PHP中文网2741 天前996

全部回覆(4)我來回復

  • 阿神

    阿神2017-04-18 10:27:17

    看了一下網站回傳的是gzip壓縮過的數據,所以要進行解碼

    # coding=utf-8
    from io import BytesIO
    import gzip
    import urllib.request
    
    url = ('http://wthrcdn.etouch.cn/weather_mini?city=%E4%B8%8A%E6%B5%B7')
    resp = urllib.request.urlopen(url)
    content = resp.read() # content是压缩过的数据
    
    buff = BytesIO(content) # 把content转为文件对象
    f = gzip.GzipFile(fileobj=buff)
    res = f.read().decode('utf-8')
    print(res)
    

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:27:17

    requests不好用嗎?

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:27:17

    建議用requeset,程式碼如下:

    import requests
    
    r = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=%E4%B8%8A%E6%B5%B7')
    print(r.text)

    回覆
    0
  • 阿神

    阿神2017-04-18 10:27:17

    不是字元編碼問題, 你看看你要求的 Respont headers

    
    
        Status Code: 200 OK
        Access-Control-Allow-Headers: *
        Access-Control-Allow-Methods: *
        Access-Control-Allow-Origin: *
        Cache-Control: must-revalidate, max-age=300
        Connection: Keep-Alive
        Content-Encoding: gzip
        Content-Length: 443
        Date: Fri, 10 Mar 2017 03:20:46 GMT
        Fw-Cache-Status: hit
        Fw-Via: HTTP MISS from 58.59.19.99, DISK HIT from 183.131.161.27
        Server: Tengine/2.1.2
    
    

    是gzip, 如果用標準庫的東西, 還需要把gzip 給解開

    回覆
    0
  • 取消回覆