本文目錄一覽:
- 1、用python編寫的獲取天氣預報的代碼總是有錯誤,求解
- 2、python怎麼自動抓取網頁上每日天氣預報
- 3、如何使用python利用api獲取天氣預報
- 4、求助:用python獲取天氣預報
- 5、Python氣象數據處理與繪圖(2):常用數據計算方法
用python編寫的獲取天氣預報的代碼總是有錯誤,求解
weatherinfo=r.json() #在json後面加上括號才能返回結果。否則只能返回函數地址。
以下python3通過:
import requests
ApiUrl=””
r=requests.get(ApiUrl)
weatherinfo=r.json()
print (weatherinfo[“weatherinfo”][“ptime”])
print (weatherinfo[“weatherinfo”][“temp2”])
08:00
5℃
python怎麼自動抓取網頁上每日天氣預報
使用到了urllib庫和bs4。bs4提供了專門針對html的解析功能,比用RE方便許多。
# coding : UTF-8import sys
reload(sys)
sys.setdefaultencoding( “utf-8” )from bs4 import BeautifulSoupimport csvimport urllibdef get_html(url):
html = urllib.urlopen(url) return html.read()def get_data(html_text):
final = []
bs = BeautifulSoup(html_text, “html.parser”)
body = bs.body
data = body.find(‘div’, {‘id’: ‘7d’})
ul = data.find(‘ul’)
li = ul.find_all(‘li’) for day in li:
temp = []
date = day.find(‘h1’).string
temp.append(date)
inf = day.find_all(‘p’)
temp.append(inf[0].string,) if inf[1].find(‘span’) is None:
temperature_highest = None
else:
temperature_highest = inf[1].find(‘span’).string
temperature_highest = temperature_highest.replace(‘C’, ”)
temperature_lowest = inf[1].find(‘i’).string
temperature_lowest = temperature_lowest.replace(‘C’, ”)
temp.append(temperature_highest)
temp.append(temperature_lowest)
final.append(temp) return finaldef write_data(data, name):
file_name = name with open(file_name, ‘a’) as f:
f_csv = csv.writer(f)
f_csv.writerows(data)if __name__ == ‘__main__’:
html_doc = get_html(”)
result = get_data(html_doc)
write_data(result, ‘weather.csv’) print result12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
運行結果保存在csv文件中
如何使用python利用api獲取天氣預報
這個和用不用python沒啥關係,是數據來源的問題。調用淘寶API,使用 api相關接口獲得你想要的內容,我 記得api中有相關的接口,你可以看一下接口的說明。用python做爬蟲來進行頁面數據的獲取。希望能幫到你。
求助:用python獲取天氣預報
# 獲取溫度、濕度、風力等
WEATHER_URL_A = “”
# 獲取天氣狀況、最大/小溫度等
WEATHER_URL_B = “”
# 獲取未來7天天氣數據
WEATHER_URL_C = “”
URL里%s指城市對應的代碼。詳細參考:
不過這篇文章里有的接口已經不能用了。
上面我給的三個URL里,前兩個直接返回json格式數據;第三個返回是一個頁面,需要自己從頁面里提取想要的信息。
Python氣象數據處理與繪圖(2):常用數據計算方法
對於氣象繪圖來講,第一步是對數據的處理,通過各類公式,或者統計方法將原始數據處理為目標數據。
按照氣象統計課程的內容,我給出了一些常用到的統計方法的對應函數:
在計算氣候態,區域平均時均要使用到求均值函數,對應NCL中的dim_average函數,在python中通常使用np.mean()函數
numpy.mean(a, axis, dtype)
假設a為[time,lat,lon]的數據,那麼
需要特別注意的是,氣象數據中常有缺測,在NCL中,使用求均值函數會自動略過,而在python中,當任意一數與缺測(np.nan)計算的結果均為np.nan,比如求[1,2,3,4,np.nan]的平均值,結果為np.nan
因此,當數據存在缺測數據時,通常使用np.nanmean()函數,用法同上,此時[1,2,3,4,np.nan]的平均值為(1+2+3+4)/4 = 2.5
同樣的,求某數組最大最小值時也有np.nanmax(), np.nanmin()函數來補充np.max(), np.min()的不足。
其他很多np的計算函數也可以通過在前邊加‘nan’來使用。
另外,
也可以直接將a中缺失值全部填充為0。
np.std(a, axis, dtype)
用法同np.mean()
在NCL中有直接求數據標準化的函數dim_standardize()
其實也就是一行的事,根據需要指定維度即可。
皮爾遜相關係數:
相關可以說是氣象科研中最常用的方法之一了,numpy函數中的np.corrcoef(x, y)就可以實現相關計算。但是在這裡我推薦scipy.stats中的函數來計算相關係數:
這個函數缺點和有點都很明顯,優點是可以直接返回相關係數R及其P值,這避免了我們進一步計算置信度。而缺點則是該函數只支持兩個一維數組的計算,也就是說當我們需要計算一個場和一個序列的相關時,我們需要循環來實現。
其中a[time,lat,lon],b[time]
(NCL中為regcoef()函數)
同樣推薦Scipy庫中的stats.linregress(x,y)函數:
slop: 回歸斜率
intercept:回歸截距
r_value: 相關係數
p_value: P值
std_err: 估計標準誤差
直接可以輸出P值,同樣省去了做置信度檢驗的過程,遺憾的是仍需同相關係數一樣循環計算。
原創文章,作者:PCPHC,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/329807.html