微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Python 或 wget/curl 从 Google-Drive 下载整个公共文件夹,无需身份验证

如何解决使用 Python 或 wget/curl 从 Google-Drive 下载整个公共文件夹,无需身份验证

我想通过脚本(python、wget、终端等)从 Google 驱动器下载整个公共文件夹。
该程序无需身份验证即可完成,因为它是一个公共文件夹,任何拥有链接的人都可以访问。

链接示例:https://drive.google.com/drive/folders/1Gt-W8jMrADizXYGF5QZwJh_Gc8QpKflX

如果无法直接下载整个文件夹,那么只需列出其内容文件),无需身份验证就足够了,然后我就可以单独下载每个文件。如何获得这样的列表功能

注意
我发现了许多类似的讨论,但都假设是文件下载或身份验证,我找不到与该特定问题匹配的内容,例如:

解决方法

我最终应用了以下代码(经过测试,效果很好):

import urllib.request
from getfilelistpy import getfilelist
from os import path,makedirs,remove,rename

def download_googledrive_folder(remote_folder,local_dir,gdrive_api_key,debug_en):

    success = True

    if debug_en:
        print('[DEBUG] Downloading: %s --> %s' % (remote_folder,local_dir))
    else:
        try:
            resource = {
                "api_key": gdrive_api_key,"id": remote_folder.split('/')[-1].split('?')[0],"fields": "files(name,id)",}
            res = getfilelist.GetFileList(resource)
            print('Found #%d files' % res['totalNumberOfFiles'])
            destination = local_dir
            if not path.exists(destination):
                makedirs(destination)
            for file_dict in res['fileList'][0]['files']:
                print('Downloading %s' % file_dict['name'])
                if gdrive_api_key:
                    source = "https://www.googleapis.com/drive/v3/files/%s?alt=media&key=%s" % (file_dict['id'],gdrive_api_key)
                else:
                    source = "https://drive.google.com/uc?id=%s&export=download" % file_dict['id']  # only works for small files (<100MB)
                destination_file = path.join(destination,file_dict['name'])
                urllib.request.urlretrieve(source,destination_file)

        except Exception as err:
            print(err)
            success = False

    return success

我找不到实现我最初目标的方法,即在没有任何凭据/密钥的情况下从 Google-Drive 下载公共文件夹,但上面的代码对我来说是一个很好的妥协,因为它只需要一个密钥而不是完整的凭据.

请注意,此处有 2 个选项 --> 提供或不提供源网址的 Google API 密钥 (gdrive_api_key)。
根据我的经验,不带 API 密钥的选项适用于小文件(

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。