使用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编程实现语音控制电脑
Apr 01 Python
深入解析Python中的descriptor描述器的作用及用法
Jun 27 Python
python中os和sys模块的区别与常用方法总结
Nov 14 Python
win10下python3.5.2和tensorflow安装环境搭建教程
Sep 19 Python
Python rstrip()方法实例详解
Nov 11 Python
django开发post接口简单案例,获取参数值的方法
Dec 11 Python
python中嵌套函数的实操步骤
Feb 27 Python
Django模型序列化返回自然主键值示例代码
Jun 12 Python
python django 原生sql 获取数据的例子
Aug 14 Python
Python对接 xray 和微信实现自动告警
Sep 17 Python
pytorch-RNN进行回归曲线预测方式
Jan 14 Python
Django权限控制的使用
Jan 07 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中执行MYSQL事务解决数据写入不完整等情况
2014/01/07 PHP
php发送与接收流文件的方法
2015/02/11 PHP
golang 调用 php7详解及实例
2017/01/04 PHP
使用 laravel sms 构建短信验证码发送校验功能
2017/11/06 PHP
js加强的经典分页实例
2013/03/15 Javascript
jQuery中的jQuery()方法用法分析
2014/12/27 Javascript
jQuery Uploadify 上传插件出现Http Error 302 错误的解决办法
2015/12/12 Javascript
详解javascript的变量与标识符
2016/01/04 Javascript
详解vue与后端数据交互(ajax):vue-resource
2017/03/16 Javascript
React Native使用Modal自定义分享界面的示例代码
2017/10/31 Javascript
vue 运用mock数据的示例代码
2017/11/07 Javascript
详解Vue中watch的高级用法
2018/05/02 Javascript
Vue.js组件间通信方式总结【推荐】
2018/11/23 Javascript
浅谈JavaScript 代码简洁之道
2019/01/09 Javascript
微信小程序渲染性能调优小结
2019/07/30 Javascript
vue自定义组件实现双向绑定
2021/01/13 Vue.js
[02:32]DOTA2英雄基础教程 美杜莎
2014/01/07 DOTA
[00:35]DOTA2上海特级锦标赛 Newbee战队宣传片
2016/03/03 DOTA
Python中尝试多线程编程的一个简明例子
2015/04/07 Python
Python编程中使用Pillow来处理图像的基础教程
2015/11/20 Python
Python内置模块ConfigParser实现配置读写功能的方法
2018/02/12 Python
基于tensorflow加载部分层的方法
2018/07/26 Python
Python编程深度学习绘图库之matplotlib
2018/12/28 Python
python3 deque 双向队列创建与使用方法分析
2020/03/24 Python
Pyspark获取并处理RDD数据代码实例
2020/03/27 Python
python+selenium+chrome实现淘宝购物车秒杀自动结算
2021/01/07 Python
css3中用animation的steps属性制作帧动画
2019/04/25 HTML / CSS
Woolworth官网:澳洲第一大超市
2017/06/25 全球购物
关于圣诞节的广播稿
2014/01/26 职场文书
初级党校心得体会
2014/09/11 职场文书
2014年辅导员工作总结
2014/11/18 职场文书
骨干教师考核评语
2014/12/31 职场文书
乡镇保密工作承诺书
2015/05/04 职场文书
匿名信格式范文
2015/05/27 职场文书
教你使用Pandas直接核算Excel中快递费用
2021/05/12 Python
MySQL的索引你了解吗
2022/03/13 MySQL