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'))
After looking at the website, the data returned is gzip compressed data, so it needs to be decoded
Isn’t requests easy to use?
It is recommended to use requestet, the code is as follows:
It’s not a character encoding problem, just look at the Response headers you requested
It is gzip. If you use the standard library, you need to unzip it