首页 > 其他分享 >requests.exceptions.MissingSchema: Invalid URLrequests.exceptions.ConnectionError: HTTPSConnectionPo

requests.exceptions.MissingSchema: Invalid URLrequests.exceptions.ConnectionError: HTTPSConnectionPo

时间:2024-03-26 09:31:25浏览次数:20  
标签:bin HTTPSConnectionPo ProtBert ConnectionError modelFolderPath exceptions path m

代码如下,运行时出现 requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.dropbox.com', port=443): Max retries exceeded with url: /s/dm3m1o0tsv9terq/pytorch_model.bin?dl=1 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x2ab61e19a370>: Failed to establish a new connection: [Errno 101] Network is unreachable')),考虑是访问网络不行,所以直接下载需要访问的第三个文件,然后修改路径

def __download_model(self) -> None:
        # Provided by the authors of Prottrans
        modelUrl = 'https://www.dropbox.com/s/dm3m1o0tsv9terq/pytorch_model.bin?dl=1'
        configUrl = 'https://www.dropbox.com/s/d3yw7v4tvi5f4sk/bert_config.json?dl=1'
        vocabUrl = 'https://www.dropbox.com/s/jvrleji50ql5m5i/vocab.txt?dl=1'
        
        #modelUrl = "./ProtBert/pytorch_model.bin"
        #configUrl = "./ProtBert/config.json"
       # vocabUrl = "./ProtBert/vocab.txt"

        modelFolderPath = self.modelFolderPath
        modelFilePath = os.path.join(modelFolderPath, 'pytorch_model.bin')
        configFilePath = os.path.join(modelFolderPath, 'config.json')
        vocabFilePath = self.vocabFilePath

        if not os.path.exists(modelFolderPath):
            os.makedirs(modelFolderPath)

        def download_file(url, filename):
            response = requests.get(url, stream=True, verify = False)
            with tqdm.wrapattr(open(filename, "wb"), "write", miniters=1,
                               total=int(response.headers.get('content-length', 0)),
                               desc=filename) as fout:
                for chunk in response.iter_content(chunk_size=4096):
                    fout.write(chunk)

        if not os.path.exists(modelFilePath):
            download_file(modelUrl, modelFilePath)

        if not os.path.exists(configFilePath):
            download_file(configUrl, configFilePath)

        if not os.path.exists(vocabFilePath):
            download_file(vocabUrl, vocabFilePath)

下载下来后将

#将以下代码,修改为下一段
modelUrl = 'https://www.dropbox.com/s/dm3m1o0tsv9terq/pytorch_model.bin?dl=1'
configUrl = 'https://www.dropbox.com/s/d3yw7v4tvi5f4sk/bert_config.json?dl=1'
vocabUrl = 'https://www.dropbox.com/s/jvrleji50ql5m5i/vocab.txt?dl=1'
   
#修改后     
modelUrl = "./ProtBert/pytorch_model.bin"
configUrl = "./ProtBert/config.json"
vocabUrl = "./ProtBert/vocab.txt"

出现报错

requests.exceptions.MissingSchema: Invalid URL './ProtBert/pytorch_model.bin': No scheme supplied. Perhaps you meant ht://./ProtBert/pytorch_model.bin?

原因是 

requests 库在尝试对一个 URL 进行请求时,发现该 URL 没有正确的协议(scheme)前缀。在给定的 URL './ProtBert/pytorch_model.bin' 中,只包含了相对路径,并没有像 http:// 或者 https:// 那样的协议指定部分。

requests 库通常用于通过 HTTP(S) 协议从远程服务器下载文件,而这里的 URL 看起来更像是一个本地文件路径。当它被传递给 requests 函数来准备请求时,requests 认为这是一个不完整的网络地址,从而抛出了 MissingSchema 异常。

修改整段代码为

 def __download_model(self) -> None:
        sourceModelPath = "./LM/ProtBert/pytorch_model.bin"
        sourceConfigPath = "./LM/ProtBert/config.json"
        sourceVocabPath = "./LM/ProtBert/vocab.txt"

        modelFolderPath = self.modelFolderPath
        modelFilePath = os.path.join(modelFolderPath, 'pytorch_model.bin')
        configFilePath = os.path.join(modelFolderPath, 'config.json')
        vocabFilePath = self.vocabFilePath

        if not os.path.exists(modelFolderPath):
            os.makedirs(modelFolderPath)

    # 检查目标路径下文件是否存在,若不存在则从源路径复制过来
        if not os.path.isfile(modelFilePath):
            shutil.copyfile(sourceModelPath, modelFilePath)
            assert os.path.isfile(modelFilePath), "未能成功复制 pytorch_model.bin 文件到目标路径 {}".format(modelFilePath)

        if not os.path.isfile(configFilePath):
            shutil.copyfile(sourceConfigPath, configFilePath)
            assert os.path.isfile(configFilePath), "未能成功复制 config.json 文件到目标路径 {}".format(configFilePath)

        if not os.path.isfile(vocabFilePath):
            shutil.copyfile(sourceVocabPath, vocabFilePath)
            assert os.path.isfile(vocabFilePath), "未能成功复制 vocab.txt 文件到目标路径 {}".format(vocabFilePath) 

成功运行啦!!!!

标签:bin,HTTPSConnectionPo,ProtBert,ConnectionError,modelFolderPath,exceptions,path,m
From: https://blog.csdn.net/luyanpingya/article/details/137033442

相关文章