使用python下載NCDC資料
阿新 • • 發佈:2019-01-03
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from ftplib import FTP
# ftp 伺服器連結
def ftpconnect():
ftp_server = 'ftp.ncdc.noaa.gov'
username = ''
password = ''
ftp = FTP()
ftp.set_debuglevel(2) # 開啟除錯級別2,顯示詳細資訊
ftp.connect(ftp_server, 21) # 連線
ftp.login(username, password) # 登入,如果匿名登入則用空串代替即可
return ftp
# 開始下載檔案
def downloadfile(start, end,srcpath):
ftp = ftpconnect()
# print ftp.getwelcome() #顯示ftp伺服器歡迎資訊
datapath = "/pub/data/gsod/"
while start <= end:
path = datapath + str(start)
li = ftp.nlst(path)
# 建立指定年份的目錄
path = srcpath + '/'
dir = str(start)
new_path = os.path.join(path, dir)
if not os.path.isdir(new_path):
os.makedirs(new_path)
for eachFile in li:
localpaths = eachFile.split("/")
localpath = localpaths[len(localpaths) - 1]
localpath = new_path + '/' + str(start) + '--' + localpath # 把日期放在最前面,方便排序
bufsize = 1024 # 設定緩衝塊大小
fp = open(localpath, 'wb') # 以寫模式在本地開啟檔案
ftp.retrbinary('RETR ' + eachFile, fp.write, bufsize) # 接收伺服器上檔案並寫入本地檔案
start = start + 1
ftp.set_debuglevel(0) # 關閉除錯
ftp.close()
ftp.quit() # 退出ftp伺服器
if __name__ == "__main__":
downloadfile(1950,2017,"g:/c/ncdc")