Python-找不到连接适配器



我遇到了一个令人沮丧的问题,我不知道如何解决。我想测试这个脚本,从ftp服务器下载一些东西:

import requests
import sys
import time
def downloadFile(url, directory) :
localFilename = url.split('/')[-1]
print(url)
with open(directory + '/' + localFilename, 'wb') as f:
start = time.clock()
r = requests.get(url, stream=True)
total_length = r.headers.get('content-length')
dl = 0
if total_length is None: # no content length header
f.write(r.content)
else:
for chunk in r.iter_content(1024):
dl += len(chunk)
f.write(chunk)
done = int(50 * dl / total_length)
sys.stdout.write("r[%s%s] %s bps" % ('=' * done, ' ' * (50-done), dl//(time.clock() - start)))
print("")
return (time.clock() - start)
def main() :
if len(sys.argv) > 1 :
url = sys.argv[1]
else :
url = input("Enter the URL : ")
directory = input("Where would you want to save the file ?")
time_elapsed = downloadFile(url, directory)
print( "Download complete...")
print ("Time Elapsed: " + time_elapsed)

if __name__ == "__main__" :
main()

我使用的Url是ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/CHDI/CHR2010_051010.xlsx但当我运行它时,会出现错误:

File "C:UsersPigeonAppDataLocalProgramsPythonPython36libsite-packagesrequestssessions.py", line 731, in get_adapter
raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/CHDI/CHR2010_051010.xlsx'

有人知道为什么会发生这种事吗?感谢回复

这意味着requests无法说出url中指定的协议,即FTP。通常它被用来说HTTP

在URL(实际上是URI(中,scheme指定要用于对话的协议。

在您的情况下,它是FTP,因为URI的前缀表示ftp://

请在这里查看组成URI的组件及其含义的良好分解。

有特定的库来"说话"FTP(文件传输协议(

例如标准ftplib

但如果您想/需要使用requests,您可以尝试使用请求ftp。

正如它所说,它为requests库提供了一个FTP传输适配器。

最新更新