Python:使用 url 从谷歌驱动器下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38511444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:04:03  来源:igfitidea点击:

Python: download files from google drive using url

pythondownloadgoogle-drive-apiurllib2pydrive

提问by rkatkam

I am trying to download files from google drive and all I have is the drive's url.

我正在尝试从谷歌驱动器下载文件,我所拥有的只是驱动器的 url。

I have read about google api that talks about some drive_service and MedioIO, which also requires some credentials( mainly json file/oauth). But I am unable to get any idea about how its working.

我读过关于 google api 的内容,它谈到了一些 drive_service 和 MedioIO,这也需要一些凭据(主要是 json 文件/oauth)。但我不知道它是如何工作的。

Also, tried urllib2 urlretrieve, but my case is to get files from drive. Tried 'wget' too but no use.

另外,尝试过 urllib2 urlretrieve,但我的情况是从驱动器中获取文件。也试过 'wget' 但没有用。

Tried pydrive library. It has good upload functions to drive but no download options.

试过 pydrive 库。它具有良好的驱动上传功能,但没有下载选项。

Any help will be appreciated. Thanks.

任何帮助将不胜感激。谢谢。

回答by turdus-merula

If by "drive's url" you mean the shareable linkof a file on Google Drive, then the following might help:

如果“驱动器的 url”是指Google Drive 上文件的可共享链接,那么以下内容可能会有所帮助:

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)

if __name__ == "__main__":
    file_id = 'TAKE ID FROM SHAREABLE LINK'
    destination = 'DESTINATION FILE ON YOUR DISK'
    download_file_from_google_drive(file_id, destination)

The snipped does not use pydrive, nor the Google Drive SDK, though. It uses the requestsmodule (which is, somehow, an alternative to urllib2).

不过,被剪断的文件不使用pydrive,也不使用 Google Drive SDK。它使用requests模块(不知何故,它是urllib2的替代品)。

When downloading large files from Google Drive, a single GET request is not sufficient. A second one is needed - see wget/curl large file from google drive.

从 Google Drive 下载大文件时,单个 GET 请求是不够的。需要第二个 - 请参阅来自 google drive 的 wget/curl 大文件

回答by ndrplz

Having had similar needs many times, I made an extra simple class GoogleDriveDownloaderstarting on the snippet from @user115202 above. You can find the source code here.

多次有类似的需求,我GoogleDriveDownloader从上面@user115202 的代码片段开始创建了一个额外的简单类。您可以在此处找到源代码。

You can also install it through pip:

也可以通过pip安装:

pip install googledrivedownloader

Then usage is as simple as:

那么用法很简单:

from google_drive_downloader import GoogleDriveDownloader as gdd

gdd.download_file_from_google_drive(file_id='1iytA1n2z4go3uVCwE__vIKouTKyIDjEq',
                                    dest_path='./data/mnist.zip',
                                    unzip=True)

This snippet will download an archive shared in Google Drive. In this case 1iytA1n2z4go3uVCwE__vIKouTKyIDjEqis the id of the sharable link got from Google Drive.

此代码段将下载在 Google 云端硬盘中共享的存档。在这种情况下,1iytA1n2z4go3uVCwE__vIKouTKyIDjEq是从 Google Drive 获得的可共享链接的 ID。

回答by Paddy

I recommend gdownpackage:

我推荐gdown包:

import gdown

url = 'https://drive.google.com/uc?id=0B9P1L--7Wd2vU3VUVlFnbTgtS2c'
output = 'spam.txt'
gdown.download(url, output, quiet=False) 

回答by Robin Nabel

PyDriveallows you to download a file with the function GetContentFile(). You can find the function's documentation here.

PyDrive允许您使用功能下载文件GetContentFile()。您可以在此处找到该函数的文档。

See example below:

请参阅下面的示例:

# Initialize GoogleDriveFile instance with file id.
file_obj = drive.CreateFile({'id': '<your file ID here>'})
file_obj.GetContentFile('cats.png') # Download file as 'cats.png'.

This code assumes that you have an authenticated driveobject, the docs on this can be found hereand here.

此代码假设您有一个经过身份验证的drive对象,可以在此处此处找到有关此的文档。

In the general case this is done like so:

在一般情况下,这是这样做的:

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
# Create local webserver which automatically handles authentication.
gauth.LocalWebserverAuth()

# Create GoogleDrive instance with authenticated GoogleAuth instance.
drive = GoogleDrive(gauth)

Info on silent authentication on a server can be found hereand involves writing a settings.yaml(example: here) in which you save the authentication details.

可以在此处找到有关服务器上的静默身份验证的信息,并涉及编写一个settings.yaml(示例:此处)来保存身份验证详细信息。

回答by Shivendra

# Importing [PyDrive][1] OAuth
from pydrive.auth import GoogleAuth

def download_tracking_file_by_id(file_id, download_dir):
    gauth = GoogleAuth(settings_file='../settings.yaml')
    # Try to load saved client credentials
    gauth.LoadCredentialsFile("../credentials.json")
    if gauth.credentials is None:
        # Authenticate if they're not there
        gauth.LocalWebserverAuth()
    elif gauth.access_token_expired:
        # Refresh them if expired
        gauth.Refresh()
    else:
        # Initialize the saved creds
        gauth.Authorize()
    # Save the current credentials to a file
    gauth.SaveCredentialsFile("../credentials.json")

    drive = GoogleDrive(gauth)

    logger.debug("Trying to download file_id " + str(file_id))
    file6 = drive.CreateFile({'id': file_id})
    file6.GetContentFile(download_dir+'mapmob.zip')
    zipfile.ZipFile(download_dir + 'test.zip').extractall(UNZIP_DIR)
    tracking_data_location = download_dir + 'test.json'
    return tracking_data_location

The above function downloads the file given the file_id to a specified downloads folder. Now the question remains, how to get the file_id? Simply split the url by id= to get the file_id.

上述函数将给定 file_id 的文件下载到指定的下载文件夹。现在问题仍然存在,如何获取file_id?只需通过 id= 拆分 url 即可获得 file_id。

file_id = url.split("id=")[1]

回答by Sundeep Pidugu

You can install https://pypi.org/project/googleDriveFileDownloader/

您可以安装 https://pypi.org/project/googleDriveFileDownloader/

pip install googleDriveFileDownloader

pip install googleDriveFileDownloader

And download the file, here is the sample code to download

并下载文件,这里是下载的示例代码

from googleDriveFileDownloader import googleDriveFileDownloader
a = googleDriveFileDownloader()
a.downloadFile("https://drive.google.com/uc?id=1O4x8rwGJAh8gRo8sjm0kuKFf6vCEm93G&export=download")

回答by Aidan L

This has also been described above,

这也已经在上面描述过,

   from pydrive.auth import GoogleAuth
   gauth = GoogleAuth()
   gauth.LocalWebserverAuth()
   drive = GoogleDrive(gauth)

This creates its own server too do the dirty work of authenticating

这会创建自己的服务器,也可以完成身份验证的肮脏工作

   file_obj = drive.CreateFile({'id': '<Put the file ID here>'})
   file_obj.GetContentFile('Demo.txt') 

This downloads the file

这将下载文件