我的最终目标是使用 python(例如使用 gdown)自动下载公共 GDrive 文件夹中的所有文件(每个文件大到 3G)。 经过大量尝试,我终于找到了一种使用 Google 表格中的 Google 脚本从文件夹中提取所有链接的方法,因此我确实拥有了我需要以这种格式下载的所有文件的所有链接:
https://drive.google.com/file/d/IDA/view?usp=drivesdk&resourcekey=otherIDA
https://drive.google.com/file/d/IDB/view?usp=drivesdk&resourcekey=otherIDB
https://drive.google.com/file/d/IDC/view?usp=drivesdk&resourcekey=otherIDC
...
https://drive.google.com/file/d/IDZ/view?usp=drivesdk&resourcekey=otherIDZ
然后我想用 for 循环遍历链接以下载所有文件:
import gdown
import re
regex = "([\w-]){33}|([\w-]){19}"
download_url_basename = "https://drive.google.com/uc?export=download&id="
for i, l in enumerate(links_to_download):
file_id = re.search(regex, url)[0]
gdown.download(download_url_basename + file_id, f"file_{i}")
然而我遇到了:
Permission denied: https://drive.google.com/uc?id=ID
Maybe you need to change permission over 'Anyone with the link'?
这是一个公共存储库,所以尽管我可以访问它并且有足够的权限手动下载每个文件,但我只能在查看模式下获得可共享的链接。
有没有办法自动将链接转换成可以自动下载的东西? 是故意屏蔽的吗?有什么办法可以自动完成而不是手动下载 400 个文件吗?
编辑:问题略有相关,但这个问题不是源于同一个问题,也没有提供自动处理任何事情的方法。
编辑 2: 我使用了 google drive API python SDK,使用 Google 控制台生成了服务帐户,激活了 OAuth2 并生成了 OAuth2 json 凭据来构建 drive_service 对象:
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from google.auth.transport.requests import Request
import io
import re
SCOPES = ['https://www.googleapis.com/auth/drive']
CLIENT_SECRET_FILE = "myjson.json"
authorized_port = 6006 # authorize URI redirect on the console
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
cred = flow.run_local_server(port=authorized_port)
drive_service = build("drive", "v3", credentials=cred)
download_url_basename = "https://drive.google.com/uc?id="
regex = "([\w-]){33}|([\w-]){19}"
for i, l in enumerate(links_to_download):
url = l
file_id = re.search(regex, url)[0]
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
但是我现在得到:
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/fileId?alt=media returned "File not found: fileID.". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: fileId.', 'locationType': 'parameter', 'location': 'fileId'}]">
找到一个相关的question 有什么想法吗?
好的,感谢 Google API,我终于能够让它工作了!
从获取文件夹内的链接列表到下载它们的整个过程太麻烦了,我可能有一天会写一篇博文:
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from google.auth.transport.requests import Request
import io
import re
SCOPES = ['https://www.googleapis.com/auth/drive']
CLIENT_SECRET_FILE = "myjson.json"
authorized_port = 6006 # authorize URI redirect on the console
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
cred = flow.run_local_server(port=authorized_port)
drive_service = build("drive", "v3", credentials=cred)
regex = "(?<=https://drive.google.com/file/d/)[a-zA-Z0-9]+"
for i, l in enumerate(links_to_download):
url = l
file_id = re.search(regex, url)[0]
request = drive_service.files().get_media(fileId=file_id)
fh = io.FileIO(f"file_{i}", mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
@jeandut 我正在尝试下载一个大文件夹(在 gdrive 上上传的课程)。我无法下载它。你能帮我吗?这是驱动器链接 - https://drive.google.com/drive/folders/1NVqQApjO-u881sMTSNubNyn-AogC1q04
@MelihDal 我仔细阅读了您关于添加 &confirm=t 的回答,但它引发了文件夹错误,我这样做了,gdown“https://drive.google.com/drive/folders/1NVqQApjO-u881sMTSNubNyn-AogC1q04&confirm=t ”。你能帮帮我吗?