windows使用python调用wget批处理下载数据

wget是linux/unix下通常使用的下载http/ftp的数据,使用非常方便,其实wget目前经过编译,也可在windows下使用。最近需要下载大量的遥感数据,使用了python写了批处理下载程序,使用的是urllib的urlretrieve进行下载,数据下载还可以,但是界面交互性不好看。就根据自己在linux下载数据进行了改进。

1. wget在windows下的安装:

从网站下载wget for windows工具(http://gnuwin32.sourceforge.net/packages/wget.htm),解压后将wget.exe拷贝至system32下,然后从cmd中键入wget,安装成功提示如下:

image

2. python批处理脚本下载

下载的大量遥感水色数据来自于NASA OBPG网站(http://oceandata.sci.gsfc.nasa.gov),根据Order之后的数据列表,保存至txt,之后构建下载网址及字符串;而后使用suprocess.call进行下载。

image

 

代码如下:

#http://oceandata.sci.gsfc.nasa.gov/cgi/getfile/A2005067190000.L0_LAC.bz2

#!/usr/bin/env python
#coding:utf-8
import os
import os.path
import subprocess

def retrieving_obpg(filelist,outpath):
    '''Download data'''
    f = open(filelist,'r')
    log= open(os.path.splitext(filelist)[0]+'_log.txt','w')
    os.chdir(outpath)
    print(os.curdir)
    for i in f:
        try:
            each_item = str(i.strip())
            cmd = 'wget http://oceandata.sci.gsfc.nasa.gov/cgi/getfile/'+each_item    
            print(cmd)            
            if not os.path.exists(outpath+each_item):
                status = subprocess.call(cmd)
                if status !=0:
                    log.write('\nFailed:'+each_item)
                    continue
                log.write('\nSuccess:'+each_item)
            log.flush()
        except:
            log.write('\nFailed:'+each_item)
            continue        
    f.close()
    log.close()


if __name__  =='__main__':        
        import glob
        outpath = 'F:\\卫星数据\\MODIS\\'
        for filelist in glob.glob(r'F:\卫星数据\MODIS\filelists\*m2s.txt'):
            retrieving_obpg(filelist,outpath)
        print('END')

posted on 2015-12-23 09:57  未济的Lakers  阅读(16438)  评论(1编辑  收藏  举报

导航