使用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中的filter()函数的用法
Apr 27 Python
在Python中处理XML的教程
Apr 29 Python
python开发之thread线程基础实例入门
Nov 11 Python
Python内置数据结构与操作符的练习题集锦
Jul 01 Python
Python基于Socket实现的简单聊天程序示例
Aug 05 Python
Python回文字符串及回文数字判定功能示例
Mar 20 Python
tensorflow实现简单的卷积网络
May 24 Python
200行python代码实现2048游戏
Jul 17 Python
Python多继承以及MRO顺序的使用
Nov 11 Python
python读取ini配置文件过程示范
Dec 23 Python
python 项目目录结构设置
Feb 14 Python
套娃式文件夹如何通过Python批量处理
Aug 23 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
dedecms 制作模板中使用的全局标记图文教程
2007/03/11 PHP
php目录遍历函数opendir用法实例
2014/11/20 PHP
用Javascript实现UTF8编码转换成gb2312编码
2006/12/22 Javascript
JS支持带x身份证号码验证函数
2008/08/10 Javascript
JS实现随机化快速排序的实例代码
2013/08/01 Javascript
jQuery截取指定长度字符串的实现原理及代码
2014/07/01 Javascript
Javascript基础教程之比较操作符
2015/01/18 Javascript
jQuery在ul中显示某个li索引号的方法
2015/03/17 Javascript
基于JavaScript短信验证码如何实现
2016/01/24 Javascript
微信小程序 教程之列表渲染
2016/10/18 Javascript
jquery 多个radio的click事件实例
2016/12/03 Javascript
80%应聘者都不及格的JS面试题
2017/03/21 Javascript
vue学习笔记之指令v-text && v-html && v-bind详解
2017/05/12 Javascript
微信小程序仿美团城市选择
2018/06/06 Javascript
vue实现循环切换动画
2018/10/17 Javascript
Vue多环境代理配置方法思路详解
2019/06/21 Javascript
解决vue-router 切换tab标签关闭时缓存问题
2020/07/22 Javascript
跟老齐学Python之有容乃大的list(4)
2014/09/28 Python
Python排序搜索基本算法之希尔排序实例分析
2017/12/09 Python
关于反爬虫的一些简单总结
2017/12/13 Python
对python判断是否回文数的实例详解
2019/02/08 Python
使用python matploblib库绘制准确率,损失率折线图
2020/06/16 Python
去除python中的字符串空格的简单方法
2020/12/22 Python
如何向接受结构参数的函数传入常数值
2016/02/17 面试题
警察思想汇报
2014/01/04 职场文书
好学生评语大全
2014/05/05 职场文书
镇人大副主席民主生活会对照检查材料思想汇报
2014/10/01 职场文书
社会实践活动报告
2015/02/05 职场文书
行政司机岗位职责
2015/04/10 职场文书
疾病证明书
2015/06/19 职场文书
党校团干班培训心得体会
2016/01/06 职场文书
初一英语教学反思
2016/02/15 职场文书
Windows 11上手初体验:任务栏和开始菜单等迎来大改
2021/11/21 数码科技
漫画「处刑少女的生存之道」第3卷封面公开
2022/03/21 日漫
使用 Koa + TS + ESLlint 搭建node服务器的过程详解
2022/05/30 NodeJs
css让页脚保持在底部位置的四种方案
2022/07/23 HTML / CSS