查到一个可以提供区域天气预报的url,说明文档在https://www.sojson.com/blog/234.html,
https://www.sojson.com/open/api/weather/json.shtml?city=%E6%88%90%E9%83%BD
打算用python从上面获取预报信息,打印下来
代码如下:
1 #python 3.6
2 #!/usr/bin/env python
3 # -*- coding:utf-8 -*-
4 __author__ = 'BH8ANK'
5
6
7 import urllib.request
8 import time
9 import ssl
10 import json
11 # #weather = "https://www.baidu.com"
12 time.sleep(3) #此处等待3秒主要是对应网页提示,三秒内只能访问一次
13 ssl._create_default_https_context = ssl._create_unverified_context
14 url = r"https://www.sojson.com/open/api/weather/json.shtml?city=%E6%88%90%E9%83%BD"#一个提供区域天气预报的url
15
16
17
18 page = urllib.request.urlopen(url)
19 # # ssl._create_default_https_context=ssl._create_unverified_context
20 html = page.read().decode("utf-8")
21
22
23 '''
24 json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)
25 (1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串)
26 (2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)
27 '''
28
29 res = json.loads(html)
30 a = json.dumps(res, ensure_ascii=False, indent=4) #将读取到的内容格式化,这样就可以看到有缩进、换行的内容
31 # print(a)
32 fp = open(r"C:\Users\xxx\Documents\weather.txt", "w",encoding='UTF-8') #将读取内容保存到文件
33 fp.write(a) #写入数据
34 fp.close() #关闭文件
35
36 res = json.loads(a) #将json转化为dict
37 # print(res)
38
39 '''
40 通过查看抓到的代码,发现dict中嵌套了dict,所以需要把对应的dict取出来
41 同样,forecast中,在list里嵌套了dict,需要仔细查看并设置中间变量
42 '''
43 today = res['data']
44 yesterday = today['yesterday']
45 forcast = today['forecast']
46 tomorrow = forcast[0] #注意看res文件内容,forecast其实是一个list,其元素才是dict
47
48 print("\n\n")
49 print("===============今日天气===============")
50 print("日期: ",res['date'])
51 print("城市: ",res['city'])
52 print("温度: ",today['wendu'])
53 print("湿度: ",today['shidu'])
54 print("PM2.5: ",today['pm25'])
55 print("空气质量: ",today['quality'])
56
57 print("\n\n")
58 print("===============昨日天气===============")
59 print("日期: ",yesterday['date'])
60 print("城市: ",res['city'])
61 print("最高温度: ",yesterday['high'])
62 print("最低温度: ",yesterday['low'])
63 print("天气状况: ",yesterday['type'])
64 # print("PM2.5: ",today['pm25'])
65 # print("空气质量: ",today['quality'])
66
67 print("\n\n")
68 print("===============明日预报===============")
69 print("日期: ",tomorrow['date'])
70 print("城市: ",res['city'])
71 print("最高温度: ",tomorrow['high'])
72 print("最低温度: ",tomorrow['low'])
73 print("天气状况: ",tomorrow['type'])
74 print("温馨提醒: ",tomorrow['notice'])
75 # print("PM2.5: ",today['pm25'])
76 # print("空气质量: ",today['quality'])
77
78
79
80
81
82 '''
83 下面是文件中的全部内容,主要是分清楚dict和list的层次,设置好中间变量,取出对应元素
84 {
85 "date": "20180426",
86 "message": "Success !",
87 "status": 200,
88 "city": "成都",
89 "count": 627,
90 "data": {
91 "shidu": "77%",
92 "pm25": 19.0,
93 "pm10": 40.0,
94 "quality": "优",
95 "wendu": "17",
96 "ganmao": "各类人群可自由活动",
97 "yesterday": {
98 "date": "25日星期三",
99 "sunrise": "06:28",
100 "high": "高温 22.0℃",
101 "low": "低温 14.0℃",
102 "sunset": "19:37",
103 "aqi": 34.0,
104 "fx": "无持续风向",
105 "fl": "<3级",
106 "type": "阴",
107 "notice": "不要被阴云遮挡住好心情"
108 },
109 "forecast": [
110 {
111 "date": "26日星期四",
112 "sunrise": "06:27",
113 "high": "高温 25.0℃",
114 "low": "低温 15.0℃",
115 "sunset": "19:38",
116 "aqi": 51.0,
117 "fx": "无持续风向",
118 "fl": "<3级",
119 "type": "多云",
120 "notice": "阴晴之间,谨防紫外线侵扰"
121 },
122 {
123 "date": "27日星期五",
124 "sunrise": "06:26",
125 "high": "高温 27.0℃",
126 "low": "低温 16.0℃",
127 "sunset": "19:39",
128 "aqi": 79.0,
129 "fx": "无持续风向",
130 "fl": "<3级",
131 "type": "多云",
132 "notice": "阴晴之间,谨防紫外线侵扰"
133 },
134 {
135 "date": "28日星期六",
136 "sunrise": "06:25",
137 "high": "高温 26.0℃",
138 "low": "低温 17.0℃",
139 "sunset": "19:39",
140 "aqi": 79.0,
141 "fx": "无持续风向",
142 "fl": "<3级",
143 "type": "多云",
144 "notice": "阴晴之间,谨防紫外线侵扰"
145 },
146 {
147 "date": "29日星期日",
148 "sunrise": "06:24",
149 "high": "高温 27.0℃",
150 "low": "低温 18.0℃",
151 "sunset": "19:40",
152 "aqi": 71.0,
153 "fx": "无持续风向",
154 "fl": "<3级",
155 "type": "多云",
156 "notice": "阴晴之间,谨防紫外线侵扰"
157 },
158 {
159 "date": "30日星期一",
160 "sunrise": "06:23",
161 "high": "高温 25.0℃",
162 "low": "低温 17.0℃",
163 "sunset": "19:41",
164 "aqi": 66.0,
165 "fx": "无持续风向",
166 "fl": "<3级",
167 "type": "小雨",
168 "notice": "雨虽小,注意保暖别感冒"
169 }
170 ]
171 }
172 }
173
174 '''
主要思路是:1,先从url获取信息;2,将信息格式化,转换为dict;3,从dict中取出对应的项
注意网络连接,目标网址会识别同一IP的访问频率,三秒以内只允许一次
关键步骤是,从url获取信息后,得到的内容是没有换行和缩进的,即非格式化数据,所以需要使用json.dumps和json.loads将数据格式化
备注:
json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)
(1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串
(2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)