This article is adapted from SDK community: sdK.cn

First, open the interface

The weather forecast Interface service uses a free interface provided by aggregated data that can be called free 100 times a day. It can be found at www.juhe.cn/docs/api/id… Registration and opening.

Python2.x call example

#! /usr/bin/python # -*- coding: utf-8 -*- import urllib, urllib2, sys, json reload(sys) sys.setdefaultencoding('utf-8') url = 'http://apis.juhe.cn/simpleWeather/query' params = { "city": "Beijing ", # query the city name of the weather, e.g. Beijing, Suzhou, Shanghai "key": Querys = urllib.urlencode(params) request = urllib2. request (url, data=querys) response = urllib2.urlopen(request) content = response.read() if (content): try: result = json.loads(content) error_code = result['error_code'] if (error_code == 0): temperature = result['result']['realtime']['temperature'] humidity = result['result']['realtime']['humidity'] info = result['result']['realtime']['info'] wid = result['result']['realtime']['wid'] direct = result['result']['realtime']['direct'] power = result['result']['realtime']['power'] aqi = Result ['result']['realtime']['aqi'] print(" Temperature: %s\n Humidity: %s\n Weather: %s\n Weather Logo: %s\n Wind Direction: %s\n Wind Power: %s\n Air Quality: %s" % (temperature, humidity, info, wid, direct, power, aqi)) else: Print (" error :%s %s" % (result['error_code'], result['reason']) except Exception as e: print(" error :%s "% e) else: Print (" error ") print(" error ")Copy the code

Python3.x call example

#! /usr/bin/python # -*- coding: Utf-8 -* -import urllib import urllib.request as request import urllib.error as error import json # def main(): utf-8 -* -import urllib import urllib.request as request import urllib.error as error import json # = 'http://apis.juhe.cn/simpleWeather/query' api_url params_dict = {" city ":" Beijing ", # of weather city name, such as: Beijing, Shanghai, suzhou "key" : Params = urllib.parse.urlencode(params_dict) try: req = request.Request(api_url, params.encode()) response = request.urlopen(req) content = response.read() if content: try: result = json.loads(content) error_code = result['error_code'] if (error_code == 0): temperature = result['result']['realtime']['temperature'] humidity = result['result']['realtime']['humidity'] info = result['result']['realtime']['info'] wid = result['result']['realtime']['wid'] direct = result['result']['realtime']['direct'] power = result['result']['realtime']['power'] aqi = Result ['result']['realtime']['aqi'] print(" Temperature: %s\n Humidity: %s\n Weather: %s\n Weather Logo: %s\n Wind Direction: %s\n Wind Power: %s\n Air Quality: %s" % ( temperature, humidity, info, wid, direct, power, aqi)) else: Print (" error :%s %s" % (result['error_code'], result['reason']) except Exception as e: print(" error :%s "% e) else: Print (" except error ") except error.HTTPError as err: print(err) except error.URLError as err: # other abnormal print (err) if __name__ = = "__main__ ': the main ()Copy the code

Return the result