使用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实现的监测服务器硬盘使用率脚本分享
Nov 07 Python
Python编程之属性和方法实例详解
May 19 Python
python避免死锁方法实例分析
Jun 04 Python
Python ValueError: invalid literal for int() with base 10 实用解决方法
Jun 21 Python
深入浅析python继承问题
May 29 Python
python+ffmpeg批量去视频开头的方法
Jan 09 Python
python 定时任务去检测服务器端口是否通的实例
Jan 26 Python
python爬虫基础教程:requests库(二)代码实例
Apr 09 Python
Python在cmd上打印彩色文字实现过程详解
Aug 07 Python
python制作朋友圈九宫格图片
Nov 03 Python
pycharm 2018 激活码及破解补丁激活方式
Sep 21 Python
Python unittest生成测试报告过程解析
Sep 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/05/08 PHP
php 模拟POST提交的2种方法详解
2013/06/17 PHP
php计算两个日期相差天数的方法
2015/03/14 PHP
php自定义时间转换函数示例
2016/12/07 PHP
php关联数组与索引数组及其显示方法
2018/03/12 PHP
PHP操作XML中XPath的应用示例
2019/07/04 PHP
Jquery ajax执行顺序 返回自定义错误信息(实例讲解)
2013/11/06 Javascript
Javascript学习笔记之 函数篇(一) : 函数声明和函数表达式
2014/06/24 Javascript
window.returnValue使用方法示例介绍
2014/07/03 Javascript
JavaScript中的数据类型转换方法小结
2015/10/26 Javascript
JavaScript html5 canvas画布中删除一个块区域的方法
2016/01/26 Javascript
Highcharts入门之简介
2016/08/02 Javascript
对javascript继承的理解
2016/10/11 Javascript
Node.js调用fs.renameSync报错(Error: EXDEV, cross-device link not permitted)
2017/12/27 Javascript
在vue里面设置全局变量或数据的方法
2018/03/09 Javascript
从零开始搭建vue移动端项目到上线的步骤
2018/10/15 Javascript
Vue路由前后端设计总结
2019/08/06 Javascript
Threejs实现滴滴官网首页地球动画功能
2020/07/13 Javascript
在Python中使用列表生成式的教程
2015/04/27 Python
Python网络编程 Python套接字编程
2017/09/13 Python
Python实现matplotlib显示中文的方法详解
2018/02/06 Python
python 获取字符串MD5值方法
2018/05/29 Python
Tensorflow 查看变量的值方法
2018/06/14 Python
浅谈解除装饰器作用(python3新增)
2018/10/15 Python
python实现维吉尼亚算法
2019/03/20 Python
Python如何对XML 解析
2020/06/28 Python
用HTML5实现手机摇一摇的功能的教程
2012/10/30 HTML / CSS
西班牙伏林航空公司:Vueling
2016/08/05 全球购物
德国网上宠物店:Zoobio
2018/05/23 全球购物
生产管理的三大手法
2013/11/11 职场文书
公证委托书大全
2014/04/04 职场文书
食品安全责任书
2014/04/15 职场文书
男性健康日的活动方案
2014/08/18 职场文书
自我工作评价范文
2015/03/06 职场文书
2016元旦文艺汇演主持词
2015/07/06 职场文书
Golang 并发编程 SingleFlight模式
2022/04/26 Golang