使用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 相关文章推荐
Python入门篇之列表和元组
Oct 17 Python
Python中的作用域规则详解
Jan 30 Python
Python多线程编程(一):threading模块综述
Apr 05 Python
整理Python 常用string函数(收藏)
May 30 Python
python解析基于xml格式的日志文件
Feb 25 Python
python简单操作excle的方法
Sep 12 Python
python反编译学习之字节码详解
May 19 Python
Python matplotlib学习笔记之坐标轴范围
Jun 28 Python
python实现两个dict合并与计算操作示例
Jul 01 Python
Python Lambda函数使用总结详解
Dec 11 Python
python 一维二维插值实例
Apr 22 Python
Python Socket TCP双端聊天功能实现过程详解
Jun 15 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 获取目录下的图片并随机显示的代码
2009/12/28 PHP
php读取文件内容的方法汇总
2015/01/24 PHP
浅谈PHP的数据库接口和技术
2016/12/09 PHP
JavaScript与C# Windows应用程序交互方法
2007/06/29 Javascript
一个简单的js动画效果代码
2010/07/20 Javascript
深入理解JavaScript系列(11) 执行上下文(Execution Contexts)
2012/01/15 Javascript
jQuery控制图片的hover效果(smartRollover.js)
2012/03/18 Javascript
原生js做的手风琴效果的导航菜单
2013/11/08 Javascript
JavaScript判断变量是否为undefined的两种写法区别
2013/12/04 Javascript
js判断当前页面在移动设备还是在PC端中打开
2016/01/06 Javascript
无需 Flash 使用 jQuery 复制文字到剪贴板
2016/04/26 Javascript
详解node如何让一个端口同时支持https与http
2017/07/04 Javascript
js 只比较时间大小的实例
2017/10/26 Javascript
js 实现复选框只能选择一项的示例代码
2018/01/23 Javascript
fetch 如何实现请求数据
2018/12/20 Javascript
three.js如何实现3D动态文字效果
2021/03/03 Javascript
Python遍历目录的4种方法实例介绍
2015/04/13 Python
python基础教程之Filter使用方法
2017/01/17 Python
python字符串str和字节数组相互转化方法
2017/03/18 Python
对pandas中两种数据类型Series和DataFrame的区别详解
2018/11/12 Python
python 实现视频流下载保存MP4的方法
2019/01/09 Python
详解python多线程之间的同步(一)
2019/04/03 Python
Python Pandas对缺失值的处理方法
2019/09/27 Python
学Python 3的理由和必要性
2019/11/19 Python
python实现自动化报表功能(Oracle/plsql/Excel/多线程)
2019/12/02 Python
python开发实例之Python的Twisted框架中Deferred对象的详细用法与实例
2020/03/19 Python
解决pyPdf和pyPdf2在合并pdf时出现异常的问题
2020/04/03 Python
vscode写python时的代码错误提醒和自动格式化的方法
2020/05/07 Python
浅谈matplotlib 绘制梯度下降求解过程
2020/07/12 Python
css3.0新属性效果在ie下的解决方案
2010/05/10 HTML / CSS
编写类String 的构造函数、析构函数和赋值函数
2012/09/09 面试题
大学拉赞助协议书范文
2014/09/26 职场文书
2014年手术室工作总结
2014/11/26 职场文书
2015年学校食堂工作总结
2015/04/22 职场文书
反四风问题学习心得体会
2016/01/22 职场文书
分析MySQL优化 index merge 后引起的死锁
2022/04/19 MySQL