Python: download a file over an FTP server

2019-01-05 01:27发布

I'm trying to download some public data files. I screenscrape to get the links to the files, which all look something like this:

ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L28POC_B.xpt

I can't find any documentation on the Requests library website.1

Thanks in advance!

8条回答
唯我独甜
2楼-- · 2019-01-05 02:13

Use urllib2. For more specifics, check out this example from doc.python.org:

Here's a snippet from the tutorial that may help

import urllib2

req = urllib2.Request('ftp://example.com')
response = urllib2.urlopen(req)
the_page = response.read()
查看更多
爷、活的狠高调
3楼-- · 2019-01-05 02:17

urllib2.urlopen handles ftp links.

查看更多
唯我独甜
4楼-- · 2019-01-05 02:18

Try using the wget library for python. You can find the documentation for it here.

    import wget
    link = 'ftp://example.com/foo.txt'
    wget.download(link)
查看更多
聊天终结者
5楼-- · 2019-01-05 02:18

urlretrieve is not work for me, and the official document said that They might become deprecated at some point in the future.

import shutil 
from urllib.request import URLopener
opener = URLopener()
url = 'ftp://ftp_domain/path/to/the/file'
store_path = 'path//to//your//local//storage'
with opener.open(url) as remote_file, open(store_path, 'wb') as local_file:
    shutil.copyfileobj(remote_file, local_file)
查看更多
forever°为你锁心
6楼-- · 2019-01-05 02:23
    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')
查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-05 02:23

As several folks have noted, requests doesn't support FTP but Python has other libraries that do. If you want to keep using the requests library, there is a requests-ftp package that adds FTP capability to requests. I've used this library a little and it does work. The docs are full of warnings about code quality though. As of 0.2.0 the docs say "This library was cowboyed together in about 4 hours of total work, has no tests, and relies on a few ugly hacks".

import requests, requests_ftp
requests_ftp.monkeypatch_session()
response = requests.get('ftp://example.com/foo.txt')
查看更多
登录 后发表回答