我正在尝试下载一些公共数据文件。为了获取文件的链接,我进行了屏幕截取,所有链接看起来都像这样:
ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L28POC_B.xpt
我找不到关于Requests库网站的任何文档。
ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L28POC_B.xpt
我找不到关于Requests库网站的任何文档。
requests
库不支持ftp://
链接。
要从FTP服务器下载文件,您可以使用urlretrieve
:
import urllib.request
urllib.request.urlretrieve('ftp://server/path/to/file', 'file')
# if you need to pass credentials:
# urllib.request.urlretrieve('ftp://username:password@server/path/to/file', 'file')
urlopen
:import shutil
import urllib.request
from contextlib import closing
with closing(urllib.request.urlopen('ftp://server/path/to/file')) as r:
with open('file', 'wb') as f:
shutil.copyfileobj(r, f)
Python 2:
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)
你可以尝试这个
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'
(我不知道任何标准的方法来转义这样的名称)。这里有更多详细信息(俄语)。如果您对FTP文件名有特定问题,可以提出单独的Stack Overflow问题。 - jfsftp.retrbinary(u"RETR täßt.jpg".encode('utf-8'), open('local.jpg', 'wb').write)
- Aidas Bendoraitis尝试使用Python的wget库。您可以在这里找到它的文档。
import wget
link = 'ftp://example.com/foo.txt'
wget.download(link)
wget.download
函数的out
参数设置文件名。 - wordsforthewiseimport 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')
正如一些人所指出的那样,requests库不支持FTP协议,但Python有其他库可以实现。如果您想继续使用requests库,可以使用requests-ftp包来添加FTP功能。我已经使用过这个库,它确实可以工作。但是文档中充满了有关代码质量的警告。截至0.2.0版本,文档中说:“这个库在大约4小时的总工作时间内被牛仔式地拼凑在一起,没有测试,并且依赖于一些丑陋的技巧。”
import requests, requests_ftp
requests_ftp.monkeypatch_session()
response = requests.get('ftp://example.com/foo.txt')
s = requests.Session()
response = s.get(...
(而不是使用requests.get
)。 - Matteo Ferlaurllib2.urlopen
可以处理FTP链接。
urlretrieve 对我来说无法工作,官方 文档 表示它们可能在未来某个时候被弃用。
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)
'ftp://username:password@server/path/to/file'
,或者使用@Rakesh的回答。如果还不行,可以提问。 - jfsrequests
是否支持ftp
? - jfswhile data := r.read(blocksize): f.write(data)
(从输入文件对象r
复制数据到输出文件f
)。 - jfs