爬取数据网站(无法直接打开)提供的数据,转换并解压字节码成为json数据,在json.cn中解析最后在console中展示出来

源码如下

from urllib.parse import quote
import urllib.request
import gzip


# 构造url,quote将文本转换为utf-8字符
cityName = input('请输入查询城市名称:')
url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + quote(cityName)

# 通过url读取数据,网站给出的是字节串数据,也就是data是字节串数据,这种数据需要解压
data_bytes = urllib.request.urlopen(url).read()

# 将字节串数据解压,成为unicode字符
data_unicode = gzip.decompress(data_bytes)

# 将unicode格式字符编码为utf-8字符,这样中文字符才可以正常显示
data = data_unicode.decode('utf-8')
# 这里的数据去掉最外边的引号可以在json.cn中解析,eval后的不行,切记!

# 上面的data是一个带有引号的json字符串,需要去掉引号,这样就成为了纯粹的json,在python中看作字典
data = eval(data)

# 展示
print('---')
print('提示:'+data['data']['ganmao'])
print('---')
print('今日天气')
print(data['data']['forecast'][0]['high'])
print(data['data']['forecast'][0]['low'])
print(data['data']['forecast'][0]['fengxiang'])
print(data['data']['forecast'][0]['type'])
print('风力'+data['data']['forecast'][0]['fengli'])
print('---')
print('明日天气')
print(data['data']['forecast'][1]['high'])
print(data['data']['forecast'][1]['low'])
print(data['data']['forecast'][1]['fengxiang'])
print(data['data']['forecast'][1]['type'])
print('风力'+data['data']['forecast'][1]['fengli'])
print('---')
print('后天天气')
print(data['data']['forecast'][2]['high'])
print(data['data']['forecast'][2]['low'])
print(data['data']['forecast'][2]['fengxiang'])
print(data['data']['forecast'][2]['type'])
print('风力'+data['data']['forecast'][2]['fengli'])

 python爬取天气_json字符串