python獲取當前天氣情況


 

 

利用 Python 從互聯網公開服務中獲取天氣預報信息。天氣信息來源網站:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
實現以下函數:
(1)獲取支持城市:
函數原型: def get_support_city(province)
參數 province:字符串,省份名稱,如“陝西”
返回值:字典類型,Key 為城市名稱,Value 為城市代碼;如:{'西安': '57036', '韓城': '53955',
'安康': '57245', '漢中': '57127', '寶雞': '57016''}
提示:采用 getSupportCity 服務。
(2)獲取天氣:
函數原型: def get_weather(name)
參數 name:字符串,城市名稱。
返回值:字符串,網站返回記錄中的“今日天氣實況”內容,如“今日天氣實況:氣溫:
3℃;風向/風力:西北風 2 級;濕度: 66%;紫外線強度:最弱。空氣質量:中。”
提示:采用 getWeatherbyCityName 服務。
參考網站:
https://blog.csdn.net/cw123458945/article/details/8146984
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
Web 可編程資源
http://www.programmableweb.com/

#!/usr/bin/python # -*- coding: UTF-8 import json import urllib.request from urllib.parse import quote import string from xml.dom import minidom #12.獲取當前天氣情況

歡迎光臨程序代寫小店https://item.taobao.com/item.htm?spm=a230r.1.14.59.255028c3ALNkZ0&id=586797758241&ns=1&abbucket=15#detail

歡迎點擊鏈接加入群聊【程序代寫-接單群】共同致富:https://jq.qq.com/?_wv=1027&k=5WxihsL 

群號:733065427

 #(1)獲取支持城市 def get_support_city(province): pass url = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName='+province url = quote (url, safe=string.printable) ret=urllib.request.urlopen(url) txt=ret.read().decode('utf-8') string_str='' key_value='' key_value_list=[] word_flag=0 # print (txt) for i in txt: string_str += i # print(string_str) if string_str.replace(' ','').replace('\t','').replace('\n','').replace('\r','')== '<string>': # print ('---------------------------string_str') word_flag = 1 if i=='>': string_str='' if word_flag==1: key_value+=i # print(key_value,'*************************************') else: key_value='' if i=='<' and word_flag==1: key_value_list.append(key_value.replace('<','').replace('>','').replace('(','').replace(')','')) key_value='' word_flag=0 # print(key_value_list) support_city={} for i in key_value_list: # print(i) word=i.split(' ') support_city[word[0]]=word[1] # print(support_city) return support_city #(2)獲取天氣 def get_weather(name): page = urllib.request.urlopen("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName="+name) lines = page.readlines() page.close() document = "" for line in lines : document = document + line.decode('utf-8') from xml.dom.minidom import parseString dom =parseString(document) strings = dom.getElementsByTagName("string") print ('今日天氣實況:',strings[10].childNodes[0].data) if __name__ == '__main__': pass province=input('請輸入要查詢的省份:') province = quote (province, safe=string.printable) support_city=get_support_city(province) print(support_city) name=input('請在上述城市中選擇城市:') # name = quote (name, safe=string.printable) name=support_city[name] city_weather = get_weather (name) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM