wzxwhd

最近在学习GPS数据软件处理,经常需要下载数据练习,反复去网站上很麻烦,于是就写了一个小小的爬虫,用的是韩国的服务器,使用了python中的ftplib库实现的

今天稍微改了一下代码,可以选择卫星系统,支持批量下载,可以选择年份年积日

忘了添加n文件下载功能了,今天补上

#encode=utf8
\'\'\'
Created on 2016年10月18日

@author: WangHui
\'\'\'
from ftplib import FTP
import os
import traceback

class DownLoadGPS(object):
    __URLon=\'210.219.33.196\'
    @staticmethod
    #形参均使用字符串表示
    def ListOName(form,year,daily,gnss=\'gps\'):
        ftp=FTP()
        ftp.connect(DownLoadGPS.__URLon,timeout=1000000)
        ftp.login()
        ftp.cwd(\'/\'+gnss+\'/data/daily/\'+year+\'/\'+daily+\'/\'+year[-2:]+\'o/\')
        return ftp
    
    @staticmethod
    def ListNName(form,year,daily,gnss=\'gps\'):
        ftp=FTP()
        ftp.connect(DownLoadGPS.__URLon)
        ftp.login()
        ftp.cwd(\'/\'+gnss+\'/data/daily/\'+year+\'/\'+daily+\'/\'+year[-2:]+\'n/\')
        return ftp 
    @staticmethod
    #下载文件
    #不考虑是什么类型的数据
    #只需要文件名和路径和ftp参数
    def DownLoadFile(ftp,path=\'D:\\\'):
        #加载第一页的数据
        DownLoadGPS.LoadPage(ftp, 1)
        while True:
            fileIndex=input(\'输入 索引(1,2,3,4,5) 开始下载,输入 n+页数(n1) 换页(按q退出) :\')
            if fileIndex==\'q\' or fileIndex==\'Q\':
                break
            if fileIndex[0]==\'n\' or fileIndex[0]==\'N\':
                DownLoadGPS.LoadPage(ftp, int(fileIndex[1:]))
            else:
                ls=fileIndex.split(\',\')
                for i in range(len(ls)):
                    try: 
                        print(\'开始下载\'+ls[i]) 
                        f=open(os.path.join(path,ftp.nlst()[int(i)-1]),\'wb\')
                        #A RETR request asks the server to send the contents of a file over the data connection already established by the client. 
                        #The RETR parameter is an encoded pathname of the file. 
                        #The file is either a binary file or a text file, depending on the most recent TYPE request.
                        ftp.retrbinary(\'RETR \'+ftp.nlst()[int(i)-1],f.write)
                    except:
                        print(\'软件异常,不过不妨事\')
    @staticmethod
    #加载第index页的数据
    def LoadPage(ftp,index,perpage=20):
        if len(ftp.nlst())==0:
            print(\'暂时没有这天数据,q退出\')
            return   
        pagecount=len(ftp.nlst())//perpage+len(ftp.nlst())%perpage
        start=(index-1)*perpage
        if start>=len(ftp.nlst()):
            start=len(ftp.nlst())-1
        end=index*perpage
        if end>len(ftp.nlst()):
            end=len(ftp.nlst())
        print(\'共%d页%d项,当前正在加载第%d页\' %(pagecount,len(ftp.nlst()),index))
        for i in range(start,end):
            print(str(i)+\' : \'+ftp.nlst()[i])
        print(\'共%d页%d项,当前是第%d页\' %(pagecount,len(ftp.nlst()),index))

if __name__==\'__main__\':
    gnss=input(\'输入卫星系统类型(gps,gnss):\').lower()
    if gnss!=\'gps\' and gnss!=\'gnss\':
        print(\'请输入正确的卫星系统,系统退出\')
        exit()
    while True:
        oorn=input(\'o文件还是n文件(o/n):\')
        try:
            year=input(\'输入年份:\')
            data=input(\'输入年积日:\')
            if oorn==\'o\':
                ftp=DownLoadGPS.ListOName(gnss, year, data,gnss)
                DownLoadGPS.DownLoadFile(ftp)
            elif oorn==\'n\':
                ftp=DownLoadGPS.ListNName(gnss, year, data,gnss)
                DownLoadGPS.DownLoadFile(ftp)
        except:
            traceback.print_exc()
            print(\'格式输入错误\')
            continue
        isnext=input(\'是否继续 y/n:\')
        if isnext==\'y\' or isnext==\'Y\':
            continue
        else:
            break

 

这是运行效果图,目前只支持gps和gnss的下载观测数据和卫星星历。

分类:

技术点:

相关文章: