使用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中self的用法
Jun 04 Python
Python在线运行代码助手
Jul 15 Python
Python解决N阶台阶走法问题的方法分析
Dec 28 Python
解决Python网页爬虫之中文乱码问题
May 11 Python
Python虚拟环境的原理及使用详解
Jul 02 Python
Django REST框架创建一个简单的Api实例讲解
Nov 05 Python
在Django中实现添加user到group并查看
Nov 18 Python
Python高级特性之闭包与装饰器实例详解
Nov 19 Python
通过实例解析python subprocess模块原理及用法
Oct 10 Python
python IP地址转整数
Nov 20 Python
Python各协议下socket黏包问题原理
Apr 12 Python
分享python函数常见关键字
Apr 26 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下实现折线图效果的代码
2007/04/28 PHP
php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
2020/04/05 PHP
Laravel框架学习笔记(一)环境搭建
2014/10/15 PHP
php发送http请求的常用方法分析
2016/11/08 PHP
thinkphp集成前端脚手架Vue-cli的教程图解
2018/08/30 PHP
jquery remove方法应用详解
2012/11/22 Javascript
LABjs、RequireJS、SeaJS的区别
2014/03/04 Javascript
javascript引擎长时间独占线程造成卡顿的解决方案
2014/12/03 Javascript
javascript 获取浏览器版本
2015/01/21 Javascript
JavaScript中函数(Function)的apply与call理解
2015/07/08 Javascript
js电话号码验证方法
2015/09/28 Javascript
jquery读写cookie操作实例分析
2015/12/24 Javascript
javascript正则表达式定义(语法)总结
2016/01/08 Javascript
AngularJs concepts详解及示例代码
2016/09/01 Javascript
利用jQuery插件imgAreaSelect实现图片上传裁剪(放大缩小)
2016/12/02 Javascript
JS实现批量上传文件并显示进度功能
2017/06/27 Javascript
关于JavaScript中高阶函数的魅力详解
2018/09/07 Javascript
Element-ui自定义table表头、修改列标题样式、添加tooltip、:render-header使用
2019/04/11 Javascript
[43:14]Liquid vs Optic 2018国际邀请赛淘汰赛BO3 第二场 8.21
2018/08/22 DOTA
Python实现的一个自动售饮料程序代码分享
2014/08/25 Python
跟老齐学Python之print详解
2014/09/28 Python
浅谈python对象数据的读写权限
2016/09/12 Python
浅谈Python编程中3个常用的数据结构和算法
2019/04/30 Python
Django的性能优化实现解析
2019/07/30 Python
基于python实现查询ip地址来源
2020/06/02 Python
Html5与App的通讯方式详解
2019/10/24 HTML / CSS
idealfit英国:世界领先的女性健身用品和运动衣物品牌
2017/11/25 全球购物
微软中国官方旗舰店:销售Surface、Xbox One、笔记本电脑、Office
2018/07/23 全球购物
日本著名化妆品零售网站:Cosme Land
2019/03/01 全球购物
Omio英国:搜索并比较便宜的巴士、火车和飞机
2019/08/27 全球购物
Steiff台湾官网:德国金耳釦泰迪熊
2019/12/26 全球购物
请解释在new与override的区别
2012/10/29 面试题
企业党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
代领学位证书毕业证书委托书
2014/09/30 职场文书
公司员工安全协议书
2014/11/21 职场文书
使用 Koa + TS + ESLlint 搭建node服务器的过程详解
2022/05/30 NodeJs