【发布时间】:2012-07-30 21:04:59
【问题描述】:
我正在尝试下载一些公共数据文件。我截屏以获取文件的链接,它们看起来都像这样:
ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L28POC_B.xpt
我在Requests library website 上找不到任何文档。
【问题讨论】:
标签: python download ftp python-requests
我正在尝试下载一些公共数据文件。我截屏以获取文件的链接,它们看起来都像这样:
ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L28POC_B.xpt
我在Requests library website 上找不到任何文档。
【问题讨论】:
标签: python download ftp python-requests
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')
或者:
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)
Python3:
import shutil
import urllib.request as request
from contextlib import closing
with closing(request.urlopen('ftp://server/path/to/file')) as r:
with open('file', 'wb') as f:
shutil.copyfileobj(r, f)
【讨论】:
'ftp://username:password@server/path/to/file' 或使用@Rakesh's answer。如果你不能让它工作,ask.
requests 是否完全支持 ftp?
ftp.login(user, passw) 调用,因此它没有加密(ftp 是一个非常古老的协议——安全性很低)。你可以试试 sftp (fabric/paramiko)。
你可以试试这个
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()
【讨论】:
b'\xff'(我不知道有任何标准方法可以转义此类名称)。这是more detail (in Russian)。如果您对 ftp 文件名有特定问题,您可以提出单独的 Stack Overflow 问题
ftp.retrbinary(u"RETR täßt.jpg".encode('utf-8'), open('local.jpg', 'wb').write)
尝试使用 python 的 wget 库。你可以找到它的文档here。
import wget
link = 'ftp://example.com/foo.txt'
wget.download(link)
【讨论】:
out 参数设置文件名。
使用urllib2。更多详情,请查看example from doc.python.org:
这是教程中的一个 sn-p,可能会有所帮助
import urllib2
req = urllib2.Request('ftp://example.com')
response = urllib2.urlopen(req)
the_page = response.read()
【讨论】:
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')
【讨论】:
正如一些人所指出的,请求不支持 FTP,但 Python 有其他库支持。如果你想继续使用 requests 库,有一个 requests-ftp 包,它为请求添加了 FTP 功能。我已经使用了这个库,它确实有效。不过,文档中充满了关于代码质量的警告。从 0.2.0 开始,文档说“这个库是在大约 4 小时的总工作时间内完成的,没有测试,并且依赖于一些丑陋的 hack”。
import requests, requests_ftp
requests_ftp.monkeypatch_session()
response = requests.get('ftp://example.com/foo.txt')
【讨论】:
s = requests.Session() response = s.get(...(不是requests.get)
urllib2.urlopen 处理 ftp 链接。
【讨论】:
如果您想利用最新 Python 版本的异步功能,您可以使用aioftp(与更流行的 aiohttp 库来自同一系列的库和开发人员)。这是取自他们的client tutorial的代码示例:
client = aioftp.Client()
await client.connect("ftp.server.com")
await client.login("user", "pass")
await client.download("tmp/test.py", "foo.py", write_into=True)
【讨论】:
urlretrieve 对我不起作用,官方document 表示它们可能会在将来的某个时候被弃用。
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)
【讨论】: