我正在尝试使用Python从我的GDrive下载一个包含50000张图片的大文件夹到本地服务器。下面的代码接收一个限制错误。有其他解决办法吗?
import gdown
url = 'https://drive.google.com/drive/folders/135hTTURfjn43fo4f?usp=sharing' # I'm showing a fake token
gdown.download_folder(url)
无法检索文件夹内容: 带有url:https://drive.google.com/drive/folders/135hTTURfjn43fo4f?usp=sharing的gdrive文件夹至少有50个文件,gdrive不能下载超过此限制的文件,如果您对此表示满意,请使用--剩余- ok标志再次运行。
发布于 2021-10-26 15:36:06
正如风筝在评论中提到的,将其与remaining_ok
标志一起使用。
gdown.download_folder(url, remaining_ok=True)
在https://pypi.org/project/gdown/中没有提到这一点,所以可能会有任何混淆。
除了警告和这个github密码。之外,remaining_ok
上的任何引用都不可用
编辑:
似乎gdown
被严格限制在50个文件之内,而且还没有找到规避它的方法。
如果不是gdown
,那么请参见下面的代码。
剧本:
import io
import os
import os.path
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google.oauth2 import service_account
credential_json = {
### Create a service account and use its the json content here ###
### https://cloud.google.com/docs/authentication/getting-started#creating_a_service_account
### credentials.json looks like this:
"type": "service_account",
"project_id": "*********",
"private_key_id": "*********",
"private_key": "-----BEGIN PRIVATE KEY-----\n*********\n-----END PRIVATE KEY-----\n",
"client_email": "service-account@*********.iam.gserviceaccount.com",
"client_id": "*********",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/service-account%40*********.iam.gserviceaccount.com"
}
credentials = service_account.Credentials.from_service_account_info(credential_json)
drive_service = build('drive', 'v3', credentials=credentials)
folderId = '### Google Drive Folder ID ###'
outputFolder = 'output'
# Create folder if not existing
if not os.path.isdir(outputFolder):
os.mkdir(outputFolder)
items = []
pageToken = ""
while pageToken is not None:
response = drive_service.files().list(q="'" + folderId + "' in parents", pageSize=1000, pageToken=pageToken,
fields="nextPageToken, files(id, name)").execute()
items.extend(response.get('files', []))
pageToken = response.get('nextPageToken')
for file in items:
file_id = file['id']
file_name = file['name']
request = drive_service.files().get_media(fileId=file_id)
### Saves all files under outputFolder
fh = io.FileIO(outputFolder + '/' + file_name, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(f'{file_name} downloaded completely.')
参考文献:
发布于 2022-01-21 01:04:50
!pip uninstall --yes gdown # After running this line, restart Colab runtime.
!pip install gdown -U --no-cache-dir
import gdown
url = r'https://drive.google.com/drive/folders/1sWD6urkwyZo8ZyZBJoJw40eKK0jDNEni'
gdown.download_folder(url)
https://stackoverflow.com/questions/69724071
复制相似问题