使用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的socket编程入门教程
Apr 23 Python
浅谈Python 集合(set)类型的操作——并交差
Jun 30 Python
Python中使用多进程来实现并行处理的方法小结
Aug 09 Python
详细分析python3的reduce函数
Dec 05 Python
Python动态导入模块的方法实例分析
Jun 28 Python
Python模拟简单电梯调度算法示例
Aug 20 Python
python3 打开外部程序及关闭的示例
Nov 06 Python
在Python中定义一个常量的方法
Nov 10 Python
简单了解python的一些位运算技巧
Jul 13 Python
Python configparser模块配置文件过程解析
Mar 03 Python
python可迭代对象去重实例
May 15 Python
六种酷炫Python运行进度条效果的实现代码
Jul 17 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
自制汽车收音机天线:收听广播的技巧和方法
2021/03/02 无线电
PHP与MYSQL中UTF8编码的中文排序实例
2014/10/21 PHP
PHP  实现等比压缩图片尺寸和大小实例代码
2016/10/08 PHP
jQuery 开天辟地入门篇一
2009/12/09 Javascript
判断控件是否已加载完成的代码
2010/02/24 Javascript
javascript中兼容主流浏览器的动态生成iframe方法
2014/05/05 Javascript
让html页面不缓存js的实现方法
2014/10/31 Javascript
使用jQuery判断Div是否在可视区域的方法 判断div是否可见
2016/02/17 Javascript
jquery实现轮播图效果
2017/02/13 Javascript
使用Vue+Django+Ant Design做一个留言评论模块的示例代码
2020/06/01 Javascript
JavaScript实现通讯录功能
2020/12/27 Javascript
[48:52]DOTA2上海特级锦标赛A组小组赛#2 Secret VS CDEC第一局
2016/02/25 DOTA
Python struct.unpack
2008/09/06 Python
深入理解python中的atexit模块
2017/03/07 Python
python使用标准库根据进程名如何获取进程的pid详解
2017/10/31 Python
详解python 拆包可迭代数据如tuple, list
2017/12/29 Python
Python实现GUI学生信息管理系统
2020/04/05 Python
Python 查找list中的某个元素的所有的下标方法
2018/06/27 Python
python实现坦克大战游戏 附详细注释
2020/03/27 Python
Pandas中resample方法详解
2019/07/02 Python
Python 3.8 新功能全解
2019/07/25 Python
对python中各个response的使用说明
2020/03/28 Python
Windows+Anaconda3+PyTorch+PyCharm的安装教程图文详解
2020/04/03 Python
Python 数据的累加与统计的示例代码
2020/08/03 Python
python 两种方法修改文件的创建时间、修改时间、访问时间
2020/09/26 Python
selenium框架中driver.close()和driver.quit()关闭浏览器
2020/12/08 Python
期末评语大全
2014/05/04 职场文书
单位在职证明书
2014/09/11 职场文书
2015年春训学习心得体会范文
2015/03/09 职场文书
2015年世界艾滋病日活动总结
2015/03/24 职场文书
2015年幼儿园中班下学期工作总结
2015/05/22 职场文书
伊索寓言读书笔记
2015/06/30 职场文书
特种设备安全管理制度
2015/08/06 职场文书
运动会广播稿50字
2015/08/19 职场文书
Java数组详细介绍及相关工具类
2022/04/14 Java/Android
Nginx如何限制IP访问只允许特定域名访问
2022/07/23 Servers