使用python3批量下載rbsp資料的示例程式碼
阿新 • • 發佈:2020-01-09
1. 原始網站
https://www.rbsp-ect.lanl.gov/data_pub/rbspa/
2. 演算法說明
進入需要下載的資料所在的目錄,獲取並解析該目錄下的資訊,解析出cdf檔名後,將cdf檔案下載到記憶體中,隨後儲存到硬碟中。程式使用python3實現。
3. 程式程式碼
#!/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())
4. 使用說明
url為遠端cdf檔案所在路徑。
path為本地儲存cdf檔案的路徑。
url和path的末尾都有“/”(Linux下情形,若是Windows,路徑分隔符為“\\”,則path末尾應為“\\”)。
5. 執行效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。