python批量下载Cmorph降水资料
生活随笔
收集整理的這篇文章主要介紹了
python批量下载Cmorph降水资料
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一個簡單的批量下載網(wǎng)站數(shù)據(jù)的腳本,主要用到urllib2等庫。
參考資料來自:https://wulc.me/2015/12/02/python%E6%89%B9%E9%87%8F%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6/
#!/usr/bin/python
#-*- coding: utf-8 -*-
import urllib2
import osdef getLegalUrl(year,mon,day,time):base_url="http://ftp.cpc.ncep.noaa.gov/precip/CMORPH_V0.x/RAW/8km-30min/"
url_preletter='CMORPH_V0.x_RAW_8km-30min_'
try:url=base_url+str(year)+'/'+str(year)+str(mon).zfill(2)+'/'+url_preletter+str(year)+str(mon).zfill(2)+str(day).zfill(2)+str(time).zfill(2)+'.gz'
f=urllib2.urlopen(url)return urlexcept urllib2.URLError:return " "
def download(url,year,mon,day,time):f=urllib2.urlopen(url)data=f.read()url_preletter = 'CMORPH_V0.x_RAW_8km-30min_'
pathgz = "E:\\CMORPH\\" + str(year) + str(mon).zfill(2) + "\\"
with open(os.path.join(pathgz,url_preletter+str(year)+str(mon).zfill(2)+str(day).zfill(2)+str(time).zfill(2)+'.gz'),'wb') as file:file.write(data)if __name__ == '__main__':days=[31,30,31,31,30]for year in range(2017,2018):for mon in range(5,10):for day in range(1,days[mon-4]):for time in range(0,24):url=getLegalUrl(year,mon,day,time)if url=="":with open("download.log",'a') as log:log.write(str(year)+str(mon).zfill(2)+str(day).zfill(2)+str(time).zfill(2)+'not found\n')else:download(url,year,mon,day,time)
總結(jié)
以上是生活随笔為你收集整理的python批量下载Cmorph降水资料的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笔记本html连接电视机黑屏是怎么回事,
- 下一篇: 计算机科学 实践调查报告,计算机专业调查