Python:通过FTP服务器下载文件

use*_*455 52 python ftp download

我正在尝试下载一些公共数据文件.我使用screenscrape来获取文件的链接,这看起来像这样:

ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L28POC_B.xpt
Run Code Online (Sandbox Code Playgroud)

我在Requests库网站上找不到任何文档.1

提前致谢!

jfs*_*jfs 63

requests 库不支持ftp链接.

要从FTP服务器下载文件,您可以:

import urllib 

urllib.urlretrieve('ftp://server/path/to/file', 'file')
# if you need to pass credentials:
#   urllib.urlretrieve('ftp://username:password@server/path/to/file', 'file')
Run Code Online (Sandbox Code Playgroud)

要么:

import shutil
import urllib2
from contextlib import closing

with closing(urllib2.urlopen('ftp://server/path/to/file')) as r:
    with open('file', 'wb') as f:
        shutil.copyfileobj(r, f)
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你,但你怎么能提供凭证? (2认同)
  • @SSHThis:尝试:''ftp://用户名:密码@ server/path/to/file'`或使用[@Rakesh的答案](http://stackoverflow.com/a/12424311/4279).如果你无法使它工作,[问](http://stackoverflow.com/questions/ask). (2认同)
  • 这里有一些urlib与请求信息:http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/ (2认同)
  • @cbare:链接的重点是什么。`requests` 完全支持 `ftp` 吗? (2认同)

Rak*_*esh 52

你可以试试这个

import ftplib

path = 'pub/Health_Statistics/NCHS/nhanes/2001-2002/'
filename = 'L28POC_B.xpt'

ftp = ftplib.FTP("Server IP") 
ftp.login("UserName", "Password") 
ftp.cwd(path)
ftp.retrbinary("RETR " + filename, open(filename, 'wb').write)
ftp.quit()
Run Code Online (Sandbox Code Playgroud)


Gau*_*ava 12

尝试使用 python 的 wget 库。您可以在此处找到它的文档。

import wget
link = 'ftp://example.com/foo.txt'
wget.download(link)
Run Code Online (Sandbox Code Playgroud)

  • 最简单并且效果很好。您还可以使用 wget.download 中的“out”参数设置文件名。 (4认同)

Par*_*ker 7

使用urllib2.有关更多细节,请查看doc.python.org中的示例:

这是教程中可能有用的片段

import urllib2

req = urllib2.Request('ftp://example.com')
response = urllib2.urlopen(req)
the_page = response.read()
Run Code Online (Sandbox Code Playgroud)


Rom*_*nov 7

    import os
    import ftplib
    from contextlib import closing

    with closing(ftplib.FTP()) as ftp:
        try:
            ftp.connect(host, port, 30*5) #5 mins timeout
            ftp.login(login, passwd)
            ftp.set_pasv(True)
            with open(local_filename, 'w+b') as f:
                res = ftp.retrbinary('RETR %s' % orig_filename, f.write)

                if not res.startswith('226 Transfer complete'):
                    print('Downloaded of file {0} is not compile.'.format(orig_filename))
                    os.remove(local_filename)
                    return None

            return local_filename

        except:
                print('Error during download from FTP')
Run Code Online (Sandbox Code Playgroud)