使用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 22 Python
python中base64加密解密方法实例分析
May 16 Python
Python使用Pycrypto库进行RSA加密的方法详解
Jun 06 Python
Python中的with语句与上下文管理器学习总结
Jun 28 Python
Django分页查询并返回jsons数据(中文乱码解决方法)
Aug 02 Python
django利用request id便于定位及给日志加上request_id
Aug 26 Python
python内置数据类型之列表操作
Nov 12 Python
python 定时器每天就执行一次的实现代码
Aug 14 Python
使用python代码进行身份证号校验的实现示例
Nov 21 Python
基于Python和C++实现删除链表的节点
Jul 06 Python
Python多线程 Queue 模块常见用法
Jul 04 Python
Flask response响应的具体使用
Jul 15 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实现自动获取生成文章主题关键词功能的深入分析
2013/06/03 PHP
探讨如何在php168_cms中提取验证码
2013/06/08 PHP
ThinkPHP中Session用法详解
2014/11/29 PHP
Yii遍历行下每列数据的方法
2016/10/17 PHP
php+redis实现商城秒杀功能
2020/11/19 PHP
在laravel中使用with实现动态添加where条件
2019/10/10 PHP
splice slice区别
2006/10/09 Javascript
autoIMG 基于jquery的图片自适应插件代码
2011/03/12 Javascript
$.format,jquery.format 使用说明
2011/07/13 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(二)人物行走的实现
2013/01/23 Javascript
通过javascript获取iframe里的值示例代码
2013/06/24 Javascript
JavaScript中的prototype和constructor简明总结
2014/04/05 Javascript
jQuery实现鼠标经过像翻页和描点链接效果
2016/08/08 Javascript
JS获取年月日时分秒的方法分析
2016/11/28 Javascript
jquery广告无缝轮播实例
2017/01/05 Javascript
如何将 jQuery 从你的 Bootstrap 项目中移除(取而代之使用Vue.js)
2017/07/17 jQuery
详解vue-cli脚手架中webpack配置方法
2018/08/22 Javascript
使用koa-log4管理nodeJs日志笔记的使用方法
2018/11/30 NodeJs
原生JS检测CSS3动画是否结束的方法详解
2019/01/27 Javascript
利用JS如何获取form表单数据
2019/12/19 Javascript
在vue中实现echarts随窗体变化
2020/07/27 Javascript
Python正则获取、过滤或者替换HTML标签的方法
2016/01/28 Python
Python彩色化Linux的命令行终端界面的代码实例分享
2016/07/02 Python
Python使用Windows API创建窗口示例【基于win32gui模块】
2018/05/09 Python
快速解决pandas.read_csv()乱码的问题
2018/06/15 Python
Python使用__new__()方法为对象分配内存及返回对象的引用示例
2019/09/20 Python
一文读懂Python 枚举
2020/08/25 Python
python利用线程实现多任务
2020/09/18 Python
html5教你做炫酷的碎片式图片切换 (canvas)
2017/07/28 HTML / CSS
canvas因为图片资源不在同一域名下而导致的跨域污染画布的解决办法
2019/01/18 HTML / CSS
联想中国官方商城:Lenovo China
2017/10/18 全球购物
奶茶专卖店创业计划书
2014/01/18 职场文书
先进个人事迹材料
2014/01/25 职场文书
群众路线教育实践活动方案
2014/10/31 职场文书
工作表扬信
2015/01/17 职场文书
Python开发简易五子棋小游戏
2022/05/02 Python