0
点赞
收藏
分享

微信扫一扫

遇到乱码的处理方法(python)

print(resp.encoding)         #查看编码方式

resp.encoding="utf8"         #修改编码方式为utf8就不会乱码了


#
import requests,json
#"丽江城市代码"101291401
url="http://www.weather.com.cn/data/sk/101291401.html"
resp=requests.get(url)
print(resp)
#print(resp.encoding) #查看编码方式
resp.encoding="utf8" #修改编码方式为utf8否则会乱码
#print(resp.encoding) #查看修改后的编码方式
#data = json.loads(resp.text)
data = resp.json() #对上一行代码的封装
#print(data,type(data))
print("城 市:",data["weatherinfo"]["city"])
print("城市代码:",data["weatherinfo"]["cityid"])
print("气 温:",data["weatherinfo"]["temp"])
print("风 向:",data["weatherinfo"]["WD"])
print("风 力:",data["weatherinfo"]["WS"])
print("湿 度:",data["weatherinfo"]["SD"])
print(data["weatherinfo"]["AP"])
print(data["weatherinfo"]["njd"])
print(data["weatherinfo"]["WSE"])
print("时 间:",data["weatherinfo"]["time"])
print(data["weatherinfo"]["sm"])
print(data["weatherinfo"]["isRadar"])
print(data["weatherinfo"]["Radar"])

运行结果:

/usr/local/bin/python3 /root/PycharmProjects/NSD211/test/demo5.py

<Response [200]>

城   市: 丽江

城市代码: 101291401

气   温: 21.9

风   向: 西北风

风   力: 4级

湿   度: 37%

760.1hPa

暂无实况

4

时   间: 17:00

7.5

1

JC_RADAR_AZ9888_JB

Process finished with exit code 0

举报

相关推荐

0 条评论