python天氣預報

2021-08-21 08:54:32 字數 3233 閱讀 5178

# 1.引入requests

import requests

def

query_now():

"""查詢實時天氣

:return

: none

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

# 2.準備url位址

url = '' % query_city

# 3.發請求,接收資料

response = requests.get(url)

# 4.轉換為字典或列表

result = response.json()

error = result['error']

# 判斷輸入的城市是否存在

if error != 0:

query_city = input('輸入城市不存在,請重新輸入:')

else:

print('~~~~~~~~~~~~~~~~~~~~~~')

results = result['results']

# 取出資料字典

data = results[0]

# 取出城市

city = data['currentcity']

print('* 查詢城市: %s' % city)

# 取出pm2.5值

pm25 = data['pm25']

print('* pm值 : %s' % pm25)

# 將字串轉換為整數 否則無法比較大小

pm25 = int(pm25)

# 通過pm2.5的值大小判斷汙染指數

if 0

<= pm25 < 35:

pollution = '優'

elif

35 <= pm25 < 75:

pollution = '良'

elif

75 <= pm25 < 115:

pollution = '輕度汙染'

elif

115

<= pm25 < 150:

pollution = '中度汙染'

elif

150

<= pm25 < 250:

pollution = '重度汙染'

elif pm25 >= 250:

pollution = '嚴重汙染'

print('* 汙染指數: %s' % pollution)

result1 = results[0]

weather_data = result1['weather_data']

data = weather_data[0]

temperature_now = data['date']

print('* 當前溫度: %s' % temperature_now)

wind = data['wind']

print('* 風向 : %s' % wind)

weather = data['weather']

print('* 天氣 : %s' % weather)

temperature = data['temperature']

print('* 溫度 : %s' % temperature)

def

query_future():

"""查詢未來三天天氣

:return

: none

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

url = '' % query_city

# 發請求,接收資料

response = requests.get(url)

# 轉換為字典或列表

result = response.json()

error = result['error']

if error != 0:

query_city = input('輸入城市不存在,請重新輸入:')

return

result_data = result['results'][0]

# 取出城市

city = result_data['currentcity']

print('********%s未來三天天氣預報********' % city)

# 取出天氣資料

weather_data = result_data['weather_data']

for x in

range(1

, len(weather_data)):

wea_dict = weather_data[x]

print('* 日期 :%s' % wea_dict['date'])

print('* 天氣 :%s' % wea_dict['weather'])

print('* 風向 :%s' % wea_dict['wind'])

print('* 溫度 :%s' % wea_dict['temperature'])

print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

print('')

while true:

print('* 歡迎使用智遊天氣查詢工具 *')

print('* 1.查詢實時天氣 ')

print('* 2.查詢未來三天天氣 ')

print('* 0.退出程式 ')

query_select = int(input('* 請輸入您的操作: '))

while query_select < 0

or query_select > 2:

query_select = int(input('輸入錯誤請重新輸入您的操作:'))

if query_select == 1:

query_now()

elif query_select == 2:

query_future()

else:

print('* 歡迎下次使用! ')

break

Flex 天氣預報

效果如下截圖 建立乙個flex專案weather 在weather.mxml中新增乙個httpwebservice如下 result onresult event resultformat e4x 定義乙個onresult函式如下 bindable private var xml xml new x...

天氣預報小偷

首先,進行小偷程式的一些初始化設定,以上 的作用分別是忽略掉所有非致命性錯誤,把小偷程式的執行超時時間設定得很長 這樣不會出現執行超時的錯誤 轉換原來預設的utf 8編碼轉換成gb2312編碼,否則直接用xmlhttp元件呼叫有中文字元的網頁得到的將是亂碼。然後呼叫xmlhttp元件建立乙個物件並進...

天氣預報完成

終於把這個天氣預報搞好了,不過功能很簡單,目前只能查當天當地城市5天天氣。文章末尾附城市列表資料庫檔案 其實後台資料庫都已經做好了,按城市和拼音查詢的介面也有,自動定位模組也寫好了,只是懶的搞了,自己用用就算了。不過明顯比墨跡的啟動快,墨跡由於是商業軟體,出於商業上考慮,加了n多的內容。等哪天朋友有...