日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python天气查询系统有什么知识点_Python入门 天气查询程序

發布時間:2024/9/27 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python天气查询系统有什么知识点_Python入门 天气查询程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

剛入門python,借鑒了論壇上前人寫的天氣查詢程序

通過使用中國天氣網的API接口來實現

如圖:

貼上源代碼,希望大家評論交流:

import urllib.request

import gzip

import json

print('------天氣查詢------')

def get_weather_data() :

city_name = input('請輸入要查詢的城市名稱:')

url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)

url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'

#網址1只需要輸入城市名,網址2需要輸入城市代碼

#print(url1)

weather_data = urllib.request.urlopen(url1).read()

#讀取網頁數據

weather_data = gzip.decompress(weather_data).decode('utf-8')

#解壓網頁數據

weather_dict = json.loads(weather_data)

#將json數據轉換為dict數據

return weather_dict

def show_weather(weather_data):

weather_dict = weather_data

#將json數據轉換為dict數據

if weather_dict.get('desc') == 'invilad-citykey':

print('你輸入的城市名有誤,或者天氣中心未收錄你所在城市')

elif weather_dict.get('desc') =='OK':

forecast = weather_dict.get('data').get('forecast')

print('城市:',weather_dict.get('data').get('city'))

print('溫度:',weather_dict.get('data').get('wendu')+'℃ ')

print('感冒:',weather_dict.get('data').get('ganmao'))

print('風向:',forecast[0].get('fengxiang'))

print('風級:',forecast[0].get('fengli'))

print('高溫:',forecast[0].get('high'))

print('低溫:',forecast[0].get('low'))

print('天氣:',forecast[0].get('type'))

print('日期:',forecast[0].get('date'))

print('*******************************')

four_day_forecast =input('是否要顯示未來四天天氣,是/否:')

if four_day_forecast == '是' or 'Y' or 'y':

for i in range(1,5):

print('日期:',forecast[i].get('date'))

print('風向:',forecast[i].get('fengxiang'))

print('風級:',forecast[i].get('fengli'))

print('高溫:',forecast[i].get('high'))

print('低溫:',forecast[i].get('low'))

print('天氣:',forecast[i].get('type'))

print('--------------------------')

print('***********************************')

show_weather(get_weather_data())

總結

以上是生活随笔為你收集整理的python天气查询系统有什么知识点_Python入门 天气查询程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。