Baidu API Query Weather

Source: Internet
Author: User
Tags json
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.