使用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中用split()方法分割字符串的使用介绍
May 20 Python
Python 处理数据的实例详解
Aug 10 Python
python中使用%与.format格式化文本方法解析
Dec 27 Python
浅谈Pandas中map, applymap and apply的区别
Apr 10 Python
Python实现抓取HTML网页并以PDF文件形式保存的方法
May 08 Python
python matlibplot绘制3D图形
Jul 02 Python
python实现随机漫步算法
Aug 27 Python
Python + selenium + requests实现12306全自动抢票及验证码破解加自动点击功能
Nov 23 Python
python如何实现从视频中提取每秒图片
Oct 22 Python
Python使用urllib模块对URL网址中的中文编码与解码实例详解
Feb 18 Python
基于python实现简单C/S模式代码实例
Sep 14 Python
python简单验证码识别的实现过程
Jun 20 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中执行MYSQL事务解决数据写入不完整等情况
2014/01/07 PHP
php中opendir函数用法实例
2014/11/15 PHP
php将textarea数据提交到mysql出现很多空格的解决方法
2014/12/19 PHP
解决laravel上传图片之后,目录有图片,但是访问不到(404)的问题
2019/10/14 PHP
推荐自用 Javascript 缩图函数 (onDOMLoaded)……
2007/10/23 Javascript
JS 动态获取节点代码innerHTML分析 [IE,FF]
2009/11/30 Javascript
Easy.Ajax 部分源代码 支持文件上传功能, 兼容所有主流浏览器
2011/02/24 Javascript
jQuery中parents()方法用法实例
2015/01/07 Javascript
jquery实现右键菜单插件
2015/03/29 Javascript
JS实现仿QQ效果的三级竖向菜单
2015/09/25 Javascript
浅析JavaScript 箭头函数 generator Date JSON
2016/05/23 Javascript
AngularJs  E2E Testing 详解
2016/09/02 Javascript
Webpack如何引入bootstrap的方法
2017/06/17 Javascript
详解vue-router和vue-cli以及组件之间的传值
2017/07/04 Javascript
JavaScript文件的同步和异步加载的实现代码
2017/08/19 Javascript
Vue 2.0学习笔记之Vue中的computed属性
2017/10/16 Javascript
Vue实现动态创建和删除数据的方法
2018/03/17 Javascript
Vue 将后台传过来的带html字段的字符串转换为 HTML
2018/03/29 Javascript
简单明了区分escape、encodeURI和encodeURIComponent
2018/05/26 Javascript
Javascript通过控制类名更改样式
2019/05/24 Javascript
jQuery实现动态加载(按需加载)javascript文件的方法分析
2019/05/31 jQuery
基于JavaScript判断两个对象内容是否相等
2020/01/10 Javascript
JavaScript实现简单的图片切换功能(实例代码)
2020/04/10 Javascript
原生JavaScript写出Tabs标签页的实例代码
2020/07/20 Javascript
在Python中使用itertools模块中的组合函数的教程
2015/04/13 Python
python去除文件中空格、Tab及回车的方法
2016/04/12 Python
numpy中索引和切片详解
2017/12/15 Python
基于python log取对数详解
2018/06/08 Python
pandas 条件搜索返回列表的方法
2018/10/30 Python
Python 运行 shell 获取输出结果的实例
2019/01/07 Python
对Python3之方法的覆盖与super函数详解
2019/06/26 Python
Python自定义一个异常类的方法
2019/06/27 Python
Volcom法国官网:美国冲浪滑板品牌
2017/05/25 全球购物
巴西电子产品购物网站:Saldão da Informática
2018/01/09 全球购物
上班上网检讨书
2014/01/29 职场文书
详解如何使用Nginx解决跨域问题
2022/05/06 Servers