Example--Query weather (convert data to JSON format)

Source: Internet
Author: User

#-*-coding:utf-8-*-ImportRequestsImportJSONImportsysreload (SYS) sys.setdefaultencoding ('UTF8')defget_weather_data (): City='Shenzhen'URL='http://wthrcdn.etouch.cn/weather_mini?city='+City Text=requests.get (URL). Text Weather=json.loads (text)ifweather['desc'] =='OK':        Print 'City:'+ weather['Data'][' City']        Print 'Temperature:'+ weather['Data']['Wendu'] +'℃'        Print 'Cold:'+ weather['Data']['Ganmao']        Print 'Wind Direction:'+ weather['Data']['Forecast'][0]['Fengxiang']        Print 'Wind:'+ weather['Data']['Forecast'][0]['Fengli']        Print 'High Temperature:'+ weather['Data']['Forecast'][0][' High']        Print 'Low Temperature:'+ weather['Data']['Forecast'][0][' Low']        Print 'weather:'+ weather['Data']['Forecast'][0]['type']        Print 'Date:'+ weather['Data']['Forecast'][0]['Date']        Print '*'* 50 whileTrue:key= Raw_input ('show weather conditions for the next four days? (y/n)')            ifKey = ='Y':                 Break            elifKey = ='N': Exit () forIinchRange (1, 5):            Print 'Date:'+ weather['Data']['Forecast'][i]['Date']            Print 'Wind Direction:'+ weather['Data']['Forecast'][i]['Fengxiang']            Print 'Wind:'+ weather['Data']['Forecast'][i]['Fengli']            Print 'High Temperature:'+ weather['Data']['Forecast'][i][' High']            Print 'Low Temperature:'+ weather['Data']['Forecast'][i][' Low']            Print 'weather:'+ weather['Data']['Forecast'][i]['type']            Print('-'* 50)    Else:        Print 'The city you entered is wrong, or the city you entered is not included in the weather center. '        if __name__=='__main__': Get_weather_data ()

Example--Query weather (convert data to JSON format)

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.