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)
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)
使用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)
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)