4

我正在尝试使用 python 的 pydrive 模块自动从谷歌驱动器下载一个简单的文本文件。我不断收到以下错误:

回溯(最后一次调用):文件“C:\GIS\AVGOPS\Scripts\GoogleDrive_Test.py”,第 20 行,在 item.GetContentFile(r'C:\Users\pfilyer\Desktop\googedrive\' + item['标题']) 文件“C:\Python27\ArcGIS10.4\lib\site-packages\pydrive\files.py”,第 210 行,在 GetContentFile self.FetchContent(mimetype, remove_bom) 文件“C:\Python27\ArcGIS10. 4\lib\site-packages\pydrive\files.py”,第 43 行,在 _decorated 返回 decoratee(self, *args, **kwargs) 文件“C:\Python27\ArcGIS10.4\lib\site-packages\pydrive \files.py",第 265 行,在 FetchContent 'No downloadLink/exportLinks for mimetype found in metadata') FileNotDownloadableError: No downloadLink/exportLinks for mimetype found in metadata

有什么建议么?

import pydrive
from pydrive.drive import GoogleDrive

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LoadCredentialsFile(r"C:\Users\XXXXX\.credentials\drive-python-quickstart.json")
drive = GoogleDrive(gauth)

print "Auth Success"

folder_id = '0BxbuUXtrs7adSFFYMG0zS3VZNFE'
lister = drive.ListFile({'q': "'%s' in parents" % folder_id}).GetList()
for item in lister:
    print item['title']
    item.GetContentFile(r'C:\Users\XXXXX\Desktop\googedrive\\' +    item['title'])
4

4 回答 4

12

您可能正在尝试下载 google 文档、电子表格或其他任何非普通文件。

不久前,当我尝试下载 mimeType 文件时,我遇到了完全相同的错误:application/vnd.google-apps.document。下载前必须将文档导出为其他格式。检查这个:

import pydrive
from pydrive.drive import GoogleDrive

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LoadCredentialsFile(r"C:\Users\XXXXX\.credentials\drive-python-    quickstart.json")
drive = GoogleDrive(gauth)

print "Auth Success"

folder_id = '0BxbuUXtrs7adSFFYMG0zS3VZNFE'
lister = drive.ListFile({'q': "'%s' in parents" % folder_id}).GetList()
for item in lister:
    print(item['title'])
    # this should tell you which mimetype the file you're trying to download 
    # has. 
    print('title: %s, mimeType: %s' % (item['title'], item['mimeType']))
    mimetypes = {
        # Drive Document files as PDF
        'application/vnd.google-apps.document': 'application/pdf',

        # Drive Sheets files as MS Excel files.
        'application/vnd.google-apps.spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

    # see https://developers.google.com/drive/v3/web/mime-types
    }
    download_mimetype = None
    if file['mimeType'] in mimetypes:
        download_mimetype = mimetypes[file['mimeType']]
        file.GetContentFile(file['title'], mimetype=download_mimetype)

        item.GetContentFile(r'C:\Users\XXXXX\Desktop\googedrive\\' + item['title'], mimetype=download_mimetype)
    else: 
        item.GetContentFile(r'C:\Users\XXXXX\Desktop\googedrive\\' + item['title'])

这应该有效。

于 2017-09-12T06:58:03.923 回答
1

当您将文档上传到谷歌驱动器时,请确保在谷歌驱动器设置中未选中“将文档转换为谷歌文档编辑器格式”选项。这可确保您上传的文件与本地文件的格式相同(例如,.csv、.txt 等)。对我来说,这行得通。

在此处输入图像描述

于 2019-01-28T19:38:43.450 回答
1

从这个线程中的示例构建,这里有一个脚本,它遍历 gdrive 文件夹的所有子文件夹并下载文件夹结构和其中的所有文件。负责处理 gdrive 文档、电子表格和演示文稿。如果文件失败则记录:)

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import os

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

MIMETYPES = {
        # Drive Document files as MS dox
        'application/vnd.google-apps.document': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        # Drive Sheets files as MS Excel files.
        'application/vnd.google-apps.spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        # Drive presentation as MS pptx
        'application/vnd.google-apps.presentation': 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
        # see https://developers.google.com/drive/v3/web/mime-types
    }
EXTENSTIONS = {
        'application/vnd.google-apps.document': '.docx',
        'application/vnd.google-apps.spreadsheet': '.xlsx',
        'application/vnd.google-apps.presentation': '.pptx'
}

f = open("failed.txt","w+")
folder_id = '<folder_id_from_browser_address_bar>'
root = 'drive_download'
os.mkdir(root)

def escape_fname(name):
    return name.replace('/','_')

def search_folder(folder_id, root):
    file_list = drive.ListFile({'q': "'%s' in parents and trashed=false" % folder_id}).GetList()
    for file in file_list:
        # print('title: %s, id: %s, kind: %s' % (file['title'], file['id'], file['mimeType']))
        # print(file)
        if file['mimeType'].split('.')[-1] == 'folder':
            foldername = escape_fname(file['title'])
            create_folder(root,foldername)
            search_folder(file['id'], '{}{}/'.format(root,foldername))
        else:
            download_mimetype = None
            filename = escape_fname(file['title'])
            filename = '{}{}'.format(root,filename)
            try:
                print('DOWNLOADING:', filename)
                if file['mimeType'] in MIMETYPES:
                    download_mimetype = MIMETYPES[file['mimeType']]

                    file.GetContentFile(filename+EXTENSTIONS[file['mimeType']], mimetype=download_mimetype)
                else:
                    file.GetContentFile(filename)
            except:
                print('FAILED')
                f.write(filename+'\n')

def create_folder(path,name):
    os.mkdir('{}{}'.format(path,escape_fname(name)))

search_folder(folder_id,root+'/')
f.close() 
于 2019-10-02T18:19:30.267 回答
0

在我的情况下,这是因为有多个文件具有相同的名称和扩展名但不同的 ID,我删除了一个并且代码有效

于 2019-03-17T12:36:57.343 回答