使用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面向对象编程之继承与多态详解
Jan 16 Python
pycharm 将django中多个app放到同个文件夹apps的处理方法
May 30 Python
Python 类的特殊成员解析
Jun 20 Python
详解Python 数据库的Connection、Cursor两大对象
Jun 25 Python
Numpy之random函数使用学习
Jan 29 Python
python 多线程重启方法
Feb 18 Python
python文件转为exe文件的方法及用法详解
Jul 08 Python
Python列表的切片实例讲解
Aug 20 Python
Python序列对象与String类型内置方法详解
Oct 22 Python
Python selenium文件上传下载功能代码实例
Apr 13 Python
Python collections.deque双边队列原理详解
Oct 05 Python
5 分钟读懂Python 中的 Hook 钩子函数
Dec 09 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密码生成类实例
2014/09/24 PHP
php实现批量修改文件名称的方法
2016/07/23 PHP
ThinkPHP的SAE开发相关注意事项详解
2016/10/09 PHP
详解PHP序列化和反序列化原理
2018/01/15 PHP
tp5框架使用composer实现日志记录功能示例
2019/01/10 PHP
IE7提供XMLHttpRequest对象为兼容
2007/03/08 Javascript
基于jquery实现的鼠标拖拽元素复制并写入效果
2011/08/23 Javascript
javascript数字格式化通用类 accounting.js使用
2012/08/24 Javascript
分享jQuery插件的学习笔记
2016/01/14 Javascript
JavaScript深度复制(deep clone)的实现方法
2016/02/19 Javascript
使用BootStrap和Metroui设计的metro风格微网站或手机app界面
2016/10/21 Javascript
微信小程序 用户数据解密详细介绍
2017/01/09 Javascript
原生js实现水平方向无缝滚动
2017/01/10 Javascript
Node中使用ES6语法的基础教程
2018/01/05 Javascript
关于ES6箭头函数中的this问题
2018/02/27 Javascript
vue路由懒加载的实现方法
2018/03/12 Javascript
AngularJS中的作用域实例分析
2018/05/16 Javascript
vue的$http的get请求要加上params操作
2020/11/12 Javascript
Python正则表达式非贪婪、多行匹配功能示例
2017/08/08 Python
python下实现二叉堆以及堆排序的示例
2017/09/29 Python
python和shell获取文本内容的方法
2018/06/05 Python
初探利用Python进行图文识别(OCR)
2019/02/26 Python
深度辨析Python的eval()与exec()的方法
2019/03/26 Python
python实现批量处理将图片粘贴到另一张图片上并保存
2019/12/12 Python
Python之Class&Object用法详解
2019/12/25 Python
python GUI库图形界面开发之PyQt5动态加载QSS样式文件
2020/02/25 Python
Python爬虫JSON及JSONPath运行原理详解
2020/06/04 Python
Html5移动端适配IphoneX等机型的方法
2019/06/25 HTML / CSS
捷克体育用品购物网站:D-sport
2017/12/28 全球购物
捷克购买家具网站:JENA nábytek
2020/03/19 全球购物
预备党员思想汇报范文
2014/01/11 职场文书
项目总经理岗位职责
2014/02/14 职场文书
家长学校实施方案
2014/03/15 职场文书
村党支部公开承诺书
2014/05/29 职场文书
学校领导干部民主生活会整改方案
2014/09/29 职场文书
2015年高三教学工作总结
2015/07/21 职场文书