从谷歌云端硬盘下载文件

1 投票
2 回答
43 浏览
提问于 2025-04-13 02:24

我正在尝试使用 gdown 从谷歌云盘下载一个文件,方法如下:

file_id = url.split('=')[-1]
gdrive_url = f'https://drive.google.com/uc?id={file_id}'
                
output_path = os.path.join(extract_to, 'file.tgz')
gdown.download(gdrive_url, output_path, quiet=False)           

但是,我总是收到这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_431/1011135980.py in <cell line: 5>()
      3 extract_to = '../fonts_data/fonts_recommend/third_party/adobe_research'
      4 
----> 5 download_and_extract(zip_url, extract_to, gdrive=True)

/tmp/ipykernel_431/1160197662.py in download_and_extract(url, extract_to, gdrive)
     34                     tar_ref.extractall(path=extract_to)
     35             else:
---> 36                 raise ValueError('Unsupported file extension for: ' + url)
     37 
     38     print(f"Extracted contents to {extract_to}")

ValueError: Unsupported file extension for: https://drive.google.com/open?id=10GRqLu6-1JPXI8rcq23S4-4AhB6On-L6

无论我怎么尝试,都没有成功。我不太确定我漏掉了什么。这是文件的链接(我可以在本地下载):

https://drive.google.com/file/d/10GRqLu6-1JPXI8rcq23S4-4AhB6On-L6/view

2 个回答

0

最简单的方法是使用 gdown 这个库。你可以试着运行下面的示例代码,从附带的谷歌云盘链接下载文件。

import gdown

url = 'https://drive.google.com/file/d/10GRqLu6-1JPXI8rcq23S4-4AhB6On-L6/view'
url = 'https://drive.google.com/uc?id=' + url.split('/')[-2]
output = 'file_downloaded.tz'
gdown.download(url, output, quiet=False)
0

这段代码看起来是没问题的。不过,你要下载的文件可能特别大,而且可能有很多用户同时在尝试下载这个文件。如果是这样的话,你可能会遇到类似我现在看到的这个错误:

FileURLRetrievalError: Failed to retrieve file url:

    Too many users have viewed or downloaded this file recently. Please
    try accessing the file again later. If the file you are trying to
    access is particularly large or is shared with many people, it may
    take up to 24 hours to be able to view or download the file. If you
    still can't access a file after 24 hours, contact your domain
    administrator.

     You may still be able to access the file from the browser:

    https://drive.google.com/uc?id=10GRqLu6-1JPXI8rcq23S4-4AhB6On-L6

    but Gdown can't. Please check connections and permissions.

撰写回答