关键词:sentinel2,python,批量下载
首先下载并解压aria2压缩包。(公众号回复关键字'aria2'获取)
将所需数据加入至购物车后,点击下载图标,会下载一个名为products.meta4头文件,不要在购物车加超过100幅影像,不然会提示错误(可以分时间段或分星下载以避免出现上述情况),并且购物车中offline的文件应及时申请批准。
命令行的运行命令为:
aria2c --http-user="我的账号" --http-passwd="我的密码" --check-certificate=false --max-concurrent-downloads=2 -M products.meta4'
运行命令行的过程中会出现诸多bug,导致需要的影像不能全部下载,这时可以简单地写个python脚本来帮忙。
i = 1 flag = True while flag: print('第{}遍运行'.format(i)) os.system('aria2c --http-user="1784132320" --http-passwd="51522zzwlwlbb" --check-certificate=false ' '--max-concurrent-downloads=2 -M products.meta4') havedone = moving() change_meta(havedone) with open(meta_path, 'r', encoding='utf-8') as f: content = f.read() if 'file' not in content: flag = False i += 1
可以循环运行命令行指令,下载的文件会保存到aria2目录,这个过程中会产生其他不需要的文件,当完整运行完一遍命令后,执行如下操作:
首先,遍历aria2目录下的所有长度大于50的文件(这是为了防止对aria相关文件进行误操作),依次使用zipfile打开,如果未报错,就将其移动至目标目录中,如果zipfile打不开,可能是文件不完整,则将其删除。
遍历结束后,将目标目录中的文件名记录为一个列表havedone,打开并读取products.meta4文件,接下来需要对该文件进行操作,利用正则表达式匹配出meta4中各文件片段,如果文件在havedone中,则将该片段删除,直到将全部片段删除后,程序结束,这时所有的文件已全部下载到目标目录中。
def change_meta(havedone): with open(meta_path, 'r', encoding='utf-8') as f: content = f.read() pattern = re.compile( r'<file.{67,78}<hash type="MD5">.{27,38}</hash><size>.{6,14}</size><url>.{90,110}</url></file>') result = pattern.findall(content) for r in result: for h in havedone: if h in r: content = content.replace(r, '') break with open(meta_path, 'w', encoding='utf-8') as f1: f1.write(content)
以上就是用python批量下载sentinel2影像的全过程,需要完整代码的客官请移步至公众号回复关键字'sentinel'。
公众号:小猪被骑的日常