本篇文章為大家展示了使用python3在對rbsp數據進行下載,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
算法說明
進入需要下載的數據所在的目錄,獲取并解析該目錄下的信息,解析出cdf文件名后,將cdf文件下載到內存中,隨后保存到硬盤中。程序使用python3實現。
程序代碼
#!/bin/python3
# get the rbsp data
# writen by Liangjin Song on 20191219
import sys
import requests
from pathlib import Path
# the url containing the cdf files
url="https://www.rbsp-ect.lanl.gov/data_pub/rbspa/ECT/level2/2016/"
# local path to save the cdf file
path="/home/liangjin/Downloads/test/"
def main():
re=requests.get(url)
html=re.text
cdfs=resolve_cdf(html)
ncdf=len(cdfs)
if ncdf == 0:
return
print(str(ncdf) + " cdf files are detected.")
i=1
# download
for f in cdfs:
rcdf=url+f
lcdf=path+f
print(str(i)+ " Downloading " + rcdf)
download_cdf(rcdf,lcdf)
i+=1
return
# resolve the file name of cdf
def resolve_cdf(html):
cdfs=list()
head=html.find("href=")
if head == -1:
print("The cdf files not found!")
return cdfs
leng=len(html)
while head != -1:
tail=html.find(">",head,leng)
# Extract the cdf file name
cdf=html[head+6:tail-1]
head=html.find("href=",tail,leng)
if cdf.find('cdf') == -1:
continue
cdfs.append(cdf)
return cdfs
def download_cdf(rcdf,lcdf):
rfile=requests.get(rcdf)
with open(lcdf,"wb") as f:
f.write(rfile.content)
f.close()
return
if __name__ == "__main__":
lpath=Path(path)
if not lpath.is_dir():
print("Path not found: " + path)
sys.exit(0)
sys.exit(main())使用說明
url為遠程cdf文件所在路徑。
path為本地保存cdf文件的路徑。
url和path的末尾都有“/”(Linux下情形,若是Windows,路徑分隔符為“\\”,則path末尾應為“\\”)。
運行效果

上述內容就是使用python3在對rbsp數據進行下載,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。