通过身份验证从FTP链接下载数据

问题描述 投票:0回答:1

我正在尝试使用python从下面的网站下载数据。

ftp://chain.physics.unb.ca/gps/data/nvd/

When I open the link in a browser, I am redirected to the page below.

我具有适当的凭据可以访问数据。输入凭据后,将转到下一页。I am taken to the following page.

一旦我可以访问此页面,我可以在URL上循环并使用wget库下载所需的数据。数据文件的URL看起来像ftp://chain.physics.unb.ca/gps/data/nvd/arvc/2017/03/arvc060B.17_.gz

我相信我可以通过相应的循环从数据页面中将其提取出来,以使用URL在数据之间导航。我很难尝试通过FTP协议通过“凭证”页面访问URL。

请告知。

python download ftp
1个回答
0
投票

也许是这样?

# write all files in one folder, with formatted date and time, to a text file...
import ftplib
import datetime 
from datetime import datetime 

ftp = ftplib.FTP('ftp.yours.com', 'u_name', 'pswd')  

#ftp.nlst()
#directory = ftp.nlst('/emm1/')

ftp.cwd('')
ftp.retrlines('LIST')  

filenames = []  
ftp.retrlines('NLST', filenames.append)  

# writes file name and modified date and file size.
with open('C:\\your_path\\test.txt', 'w')  as f:
    for filename in filenames:  
        datetimeftp = ftp.sendcmd('MDTM ' + filename)
        modifiedTimeFtp = datetime.strptime(datetimeftp[4:], "%Y%m%d%H%M%S").strftime("%d %b %Y %H:%M:%S")
        size = ftp.size(filename)
        filesize = "{:.2f}".format(size/(1024))
        f.write(filename)
        f.write(':')
        f.write(modifiedTimeFtp)
        f.write(':')
        f.write(filesize + ' KB')
        f.write('\n')
f.close()

或者,也许这吗?

import ftplib
from ftplib import FTP
ftp = FTP()
from datetime import datetime


filenames = []
data = []

ftp = ftplib.FTP('ftp.anything.com', 'u_name', 'ps_wd')  


def get_dirs_ftp(folder=""):
    contents = ftp.nlst(folder)
    folders = []
    for item in contents:
        if "." not in item:
            folders.append(item)
    return folders
def get_all_dirs_ftp(folder=""):
    dirs = []
    new_dirs = []
    new_dirs = get_dirs_ftp(folder)
    while len(new_dirs) > 0:
        for dir in new_dirs:
            dirs.append(dir)

        old_dirs = new_dirs[:]
        new_dirs = []
        for dir in old_dirs:
            for new_dir in get_dirs_ftp(dir):
                new_dirs.append(new_dir)
    dirs.sort()
    return dirs

#allfiles = []
# get parent and child folders in directory
all_dirs = get_all_dirs_ftp()

# create a list to append metadata
dir_list = []

for dir in all_dirs:
    ftp.cwd('/'+dir+'/')
    print(dir)
    dir_list.append(dir)
    ftp.dir(dir_list.append)

    len(dir_list)


# you probably want to dump the results to a file...
outF = open('C:/your_path/filenames.csv', 'w')
for line in dir_list:
  # write line to output file
  outF.write(line)
  outF.write("\n")
outF.close()
print('Done!!')
© www.soinside.com 2019 - 2024. All rights reserved.