Import OS import urllib.request import urllib.parse Import JSON class weather (object): # Gets the URI of the city code Code_uri = "H Ttp://apistore.baidu.com/microservice/cityinfo?cityname= "# Get weather information for URI Weather_uri =" Http://apistore.baidu.com/mi croservice/weather?cityid= "# Master processing Logic def mainhandle (self): City_name = Input (" Enter the weather you want to query: ") URI = SE
Lf.code_uri + urllib.parse.quote (city_name) ret = json.loads (Urllib.request.urlopen (URI). Read (). Decode ("UTF8")) If ret[' errnum ']! = 0:print (ret[' retmsg ']) return False Else:weather_u RI = Self.weather_uri + ret[' retdata ' [' citycode '] data = Json.loads (Urllib.request.urlopen (Weather_uri). Read ( ). Decode ("UTF8")) if data[' errnum '] = = 0:ret_data = data[' retdata '] output =
"City Name:" + city_name + "\ r \ n" Output + + "Updated:" + ret_data["date" + "" + ret_data["time" + "\ r \ n" Output + = "Weather:" + ret_data["Weather"] + "[" + ret_data["WD"] + ret_data["WS" + "]\r\n" output + = "Current temperature:" + ret_data["Te
MP "] +" ("+ ret_data[" h_tmp "] +"---> "+ ret_data[" l_tmp "] +") \ r \ n "Print (output) Return True else:print (data[' errmsg ')) return False if __name__ = = "__main__ ": Weather = Weather () weather.mainhandle ()