使用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格式化字符串实例总结
Sep 28 Python
Python编程中归并排序算法的实现步骤详解
May 04 Python
Python+Socket实现基于UDP协议的局域网广播功能示例
Aug 31 Python
Python3实现的字典、列表和json对象互转功能示例
May 22 Python
详解Python发送email的三种方式
Oct 18 Python
解决Python下json.loads()中文字符出错的问题
Dec 19 Python
pyttsx3实现中文文字转语音的方法
Dec 24 Python
python统计中文字符数量的两种方法
Jan 31 Python
Python代码块及缓存机制原理详解
Dec 13 Python
pytorch获取模型某一层参数名及参数值方式
Dec 30 Python
Pandas读取csv时如何设置列名
Jun 02 Python
详解python polyscope库的安装和例程
Nov 13 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
ADODB类使用
2006/11/25 PHP
Laravel 微信小程序后端搭建步骤详解
2019/11/26 PHP
javascript 写类方式之一
2009/07/05 Javascript
Javascript和Ajax中文乱码吐血版解决方案
2009/12/21 Javascript
JQuery对id中含有特殊字符的转义处理示例
2013/09/06 Javascript
JQuery给元素绑定click事件多次执行的解决方法
2014/05/29 Javascript
php+js实现倒计时功能
2014/06/02 Javascript
详解AngularJS中的表格使用
2015/06/16 Javascript
jQuery轮播图效果精简版完整示例
2016/09/04 Javascript
使用Require.js封装原生js轮播图的实现代码
2017/06/15 Javascript
微信小程序实现topBar底部选择栏效果
2018/07/20 Javascript
在vue项目中使用md5加密的方法
2018/09/14 Javascript
用python + openpyxl处理excel2007文档思路以及心得
2014/07/14 Python
python字符串替换的2种方法
2014/11/30 Python
Django unittest 设置跳过某些case的方法
2018/12/26 Python
Python中的集合介绍
2019/01/28 Python
Pytorch中膨胀卷积的用法详解
2020/01/07 Python
在django项目中导出数据到excel文件并实现下载的功能
2020/03/13 Python
Python私有属性私有方法应用实例解析
2020/09/15 Python
python3列表删除大量重复元素remove()方法的问题详解
2021/01/04 Python
CSS3+HTML5+JS 实现一个块的收缩与展开动画效果
2020/11/17 HTML / CSS
详解Html5 Canvas画线有毛边解决方法
2018/03/01 HTML / CSS
美国女性卫生用品公司:Thinx
2017/06/30 全球购物
什么时候用assert
2015/05/08 面试题
J2EE系统只能是基于web
2015/09/08 面试题
幼师专业毕业生自荐信
2013/09/29 职场文书
工业设计专业个人求职信范文
2013/12/28 职场文书
女方婚礼新郎答谢词
2014/01/11 职场文书
大学秋游活动方案
2014/02/11 职场文书
学雷锋树新风演讲稿
2014/05/10 职场文书
学校安全工作汇报材料
2014/08/16 职场文书
初中国旗下的演讲稿
2014/08/28 职场文书
工作总结之小学教师体育工作范文(3篇)
2019/10/07 职场文书
Vue vee-validate插件的简单使用
2021/06/22 Vue.js
Pygame Event事件模块的详细示例
2021/11/17 Python
Python创建SQL数据库流程逐步讲解
2022/09/23 Python