使用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 str与repr的区别
Mar 23 Python
python选择排序算法实例总结
Jul 01 Python
windows下安装Python和pip终极图文教程
Mar 05 Python
python下setuptools的安装详解及No module named setuptools的解决方法
Jul 06 Python
python版opencv摄像头人脸实时检测方法
Aug 03 Python
python 生成图形验证码的方法示例
Nov 11 Python
Python实现高斯函数的三维显示方法
Dec 29 Python
python3 打印输出字典中特定的某个key的方法示例
Jul 06 Python
Python实现微信翻译机器人的方法
Aug 13 Python
python针对Oracle常见查询操作实例分析
Apr 30 Python
pandas抽取行列数据的几种方法
Dec 13 Python
matplotlib绘制多子图共享鼠标光标的方法示例
Jan 08 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注入点构造代码
2008/06/14 PHP
php常用字符串比较函数实例汇总
2014/11/24 PHP
PHP中的use关键字及文件的加载详解
2016/11/28 PHP
PHP针对中英文混合字符串长度判断及截取方法示例
2017/03/31 PHP
详解提高使用Java反射的效率方法
2019/04/29 PHP
最短的javascript:地址栏载入脚本代码
2011/10/13 Javascript
Jquery中给animation加更多的运作效果实例
2013/09/05 Javascript
jQuery对象与DOM对象之间的相互转换
2015/03/03 Javascript
纯js三维数组实现三级联动效果
2017/02/07 Javascript
nodejs中模块定义实例详解
2017/03/18 NodeJs
JS常见创建类的方法小结【工厂方式,构造器方式,原型方式,联合方式等】
2017/04/01 Javascript
详解自定义ajax支持跨域组件封装
2018/02/08 Javascript
Vue 父子组件数据传递的四种方式( inheritAttrs + $attrs + $listeners)
2018/05/04 Javascript
nodejs初始化init的示例代码
2018/10/10 NodeJs
vue单页应用的内存泄露定位和修复问题小结
2019/08/02 Javascript
iview form清除校验状态的实现
2019/09/19 Javascript
Python Tkinter GUI编程入门介绍
2015/03/10 Python
浅析Python中的getattr(),setattr(),delattr(),hasattr()
2016/06/14 Python
Python实现拷贝多个文件到同一目录的方法
2016/09/19 Python
请不要重复犯我在学习Python和Linux系统上的错误
2016/12/12 Python
Odoo中如何生成唯一不重复的序列号详解
2018/02/10 Python
python读取和保存图片5种方法对比
2018/09/12 Python
python实现12306登录并保存cookie的方法示例
2019/12/17 Python
在Python中用GDAL实现矢量对栅格的切割实例
2020/03/11 Python
纯CSS3实现鼠标悬停提示气泡效果
2014/02/28 HTML / CSS
The Hut英国:英国领先的豪华在线百货商店
2019/07/26 全球购物
康拓普公司Java笔面试
2016/09/23 面试题
仓管员岗位职责范文
2013/11/08 职场文书
大学生最常用的自我评价
2013/12/07 职场文书
校园活动策划书范文
2014/01/10 职场文书
创建市级文明单位实施方案
2014/03/01 职场文书
公司离职证明标准格式
2014/11/18 职场文书
工程质检员岗位职责
2015/04/08 职场文书
教师节晚会主持词
2015/06/30 职场文书
导游词之河北邯郸
2019/09/12 职场文书
Python 的演示平台支持 WSGI 接口的应用
2022/04/20 Python