使用python3批量下载rbsp数据的示例代码


Posted in Python onDecember 20, 2019

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. 运行效果

使用python3批量下载rbsp数据的示例代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
opencv python 图像去噪的实现方法
Aug 31 Python
Python3的介绍、安装和命令行的认识(推荐)
Oct 20 Python
Python可变和不可变、类的私有属性实例分析
May 31 Python
PyTorch中Tensor的拼接与拆分的实现
Aug 18 Python
Python中xml和dict格式转换的示例代码
Nov 07 Python
Python3实现配置文件差异对比脚本
Nov 18 Python
Pytorch中Tensor与各种图像格式的相互转化详解
Dec 26 Python
tensorflow保持每次训练结果一致的简单实现
Feb 17 Python
git查看、创建、删除、本地、远程分支方法详解
Feb 18 Python
在python里创建一个任务(Task)实例
Apr 25 Python
pytorch加载语音类自定义数据集的方法教程
Nov 10 Python
Python 打印自己设计的字体的实例讲解
Jan 04 Python
Python使用QQ邮箱发送邮件报错smtplib.SMTPAuthenticationError
Dec 20 #Python
Python字符串、列表、元组、字典、集合的补充实例详解
Dec 20 #Python
python获取网络图片方法及整理过程详解
Dec 20 #Python
python序列化与数据持久化实例详解
Dec 20 #Python
爬虫代理池Python3WebSpider源代码测试过程解析
Dec 20 #Python
python3的UnicodeDecodeError解决方法
Dec 20 #Python
基于python调用psutil模块过程解析
Dec 20 #Python
You might like
PHP四大安全策略
2014/03/12 PHP
PHP图像处理之imagecreate、imagedestroy函数介绍
2014/11/19 PHP
PHP内核学习教程之php opcode内核实现
2016/01/27 PHP
php实现转换html格式为文本格式的方法
2016/05/16 PHP
PHP中获取文件创建日期、修改日期、访问时间的方法
2016/11/05 PHP
JQuery AJAX实现目录浏览与编辑的代码
2008/10/21 Javascript
JAVASCRIPT IE 与 FF中兼容问题小结
2009/02/18 Javascript
火狐textarea输入法的bug的触发及解决
2013/07/24 Javascript
动态标签 悬停效果 延迟加载示例代码
2013/11/21 Javascript
jQuery中trigger()与bind()用法分析
2015/12/18 Javascript
浏览器兼容的JS写法总结
2016/04/27 Javascript
js实现方块上下左右移动效果
2017/08/17 Javascript
Vee-Validate的使用方法详解
2017/09/22 Javascript
vue获取当前激活路由的方法
2018/03/17 Javascript
浅谈Vue页面级缓存解决方案feb-alive(上)
2019/04/14 Javascript
基于vue的tab-list类目切换商品列表组件的示例代码
2020/02/14 Javascript
vue 动态组件用法示例小结
2020/03/06 Javascript
js实现数据导出为EXCEL(支持大量数据导出)
2020/03/31 Javascript
Element Carousel 走马灯的具体实现
2020/07/26 Javascript
举例讲解Python中的迭代器、生成器与列表解析用法
2016/03/20 Python
python 内置函数filter
2017/06/01 Python
Python多继承顺序实例分析
2018/05/26 Python
Sanic框架配置操作分析
2018/07/17 Python
Python常见排序操作示例【字典、列表、指定元素等】
2018/08/15 Python
对python插入数据库和生成插入sql的示例讲解
2018/11/14 Python
PyQt5实现简易计算器
2020/05/30 Python
python二维码操作:对QRCode和MyQR入门详解
2019/06/24 Python
英国剑桥包中文官网:The Cambridge Satchel Company中国
2018/11/06 全球购物
Notino瑞典:购买香水和美容产品
2019/07/26 全球购物
电气工程及自动化专业自荐书范文
2013/12/18 职场文书
政法大学毕业生自荐信范文
2014/01/01 职场文书
企业给企业的表扬信
2014/01/13 职场文书
餐厅筹备计划书
2014/04/25 职场文书
电影建国大业观后感
2015/06/01 职场文书
付款证明模板
2015/06/19 职场文书
Python实现socket库网络通信套接字
2021/06/04 Python