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

时间:2016-07-21 18:09:36

标签: python download google-drive-api urllib2 pydrive

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

我已经阅读了关于google api的内容,该内容涉及一些drive_service和MedioIO,它还需要一些凭据(主要是json文件/ oauth)。但我无法知道它是如何工作的。

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

尝试了pydrive库。它具有良好的上传功能,但无需下载选项。

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

9 个答案:

答案 0 :(得分:29)

如果“驱动器的网址”是指Google云端硬盘中文件的可共享链接,则以下内容可能有所帮助:

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)

剪切不会使用 pydrive ,也不会使用Google Drive SDK。它使用requests模块(不知何故,它是 urllib2 的替代品)。

从Google云端硬盘下载大型文件时,单个GET请求是不够的。需要第二个 - 请参阅wget/curl large file from google drive

答案 1 :(得分:13)

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

您也可以通过pip安装它:

pip install googledrivedownloader

然后使用就像:

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)

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

答案 2 :(得分:2)

PyDrive允许您下载功能为GetContentFile()的文件。您可以找到该功能的文档here

见下面的例子:

# 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'.

此代码假定您拥有经过身份验证的drive对象,可以找到有关此文档的文档herehere

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

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)

可以找到有关服务器上静默身份验证的信息here,并且需要编写settings.yaml(例如:here)来保存身份验证详细信息。

答案 3 :(得分:2)

这是没有第三方库和服务帐户的简单方法。

点安装google-api-coregoogle-api-python-client

from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google.oauth2 import service_account
import io

credz = {} #put json credentials her from service account or the like
# More info: https://cloud.google.com/docs/authentication

credentials = service_account.Credentials.from_service_account_info(credz)
drive_service = build('drive', 'v3', credentials=credentials)

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_service.files().get_media(fileId=file_id)
#fh = io.BytesIO() # this can be used to keep in memory
fh = io.FileIO('file.tar.gz', 'wb') # this can be used to write to disk
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print("Download %d%%." % int(status.progress() * 100))


答案 4 :(得分:1)

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

pip install googleDriveFileDownloader

然后下载文件,这是要下载的示例代码

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

答案 5 :(得分:1)

通常来说,来自Google云端硬盘共享文件的网址如下

https://drive.google.com/file/d/1HV6vf8pB-EYnjcJcH65eGZVMa2v2tcMh/view?usp=sharing

其中1HV6vf8pB-EYnjcJcH65eGZVMa2v2tcMh对应于文件ID。

因此,您只需创建一个函数即可从URL获取文件ID,例如url = https://drive.google.com/file/d/1HV6vf8pB-EYnjcJcH65eGZVMa2v2tcMh/view?usp=sharing

def url_to_id(url):
    x = url.split("/")
    return x[5]

打印x会给出

['https:', '', 'drive.google.com', 'file', 'd', '1HV6vf8pB-EYnjcJcH65eGZVMa2v2tcMh', 'view?usp=sharing']

因此,由于要返回第6个数组值,因此使用x[5]

答案 6 :(得分:0)

上面也对此进行了描述,

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

这也会创建自己的服务器进行身份验证的肮脏工作

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

下载文件

答案 7 :(得分:0)

Observable.combineLatest(
        Observable.forkJoin([
            this.countryService.getAll(),
            this.categoryService.getAll(),
            this.sectorService.getAll()
        ]),
        this.route.paramMap.switchMap(({id}) => {
            this.formDisabled = true;
            return this.service.get(id);
        })
    )
        .subscribe(([countries, categories, sectors, contact]) => {
            //Assign the dropdown values
            this.countries = countries;
            this.categories = categories;
            this.sectors = sectors;

            //Do the rest
            this.contact = contact;
            this.form.reset(this.contact);
            this.formDisabled = false;
        })

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

# 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

答案 8 :(得分:0)

我推荐gdown包裹:

{
    "password": "pms123",
    "username": "elastic"
}