下载整个Google驱动器公共共享文件夹而不进行任何身份验证

2024-05-12 19:45:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种方法下载一个完整的谷歌驱动器文件夹从公开共享谷歌驱动器文件夹链接,而不必给我的谷歌证书。在

这个问题展示了如何下载一个公共共享的googledrive文件夹的全部内容,但是它需要Pydrive来访问你的Google凭证。在

Python: How do download entire folder from Google Drive

这段代码可以下载一个公共共享的google驱动器文件,而无需访问google凭证。在

import os  
import requests

def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

file_id = '1thX75_cMkly5btgxe4dgV6YDKnrUczZs' #google drive file id

download_file_from_google_drive(file_id, 'thefile.extension')

有没有一种方法可以在不提供google凭证的情况下下载一个公共共享的google驱动器文件夹的全部内容?在


Tags: from文件夹tokenidgetresponsedownloaddef