我试图在脚本中从谷歌驱动器下载一个文件,我这样做有点麻烦。我要下载的文件在这里。
我在网上搜了很多,终于下载了其中一个。我得到了文件的uid,较小的文件(1.6MB)下载正常,但较大的文件(3.7GB)总是重定向到一个页面,询问我是否想在不进行病毒扫描的情况下继续下载。谁能帮我跳过那个屏幕?
下面是我如何让第一个文件工作-
curl -L "https://docs.google.com/uc?export=download&id=0Bz-w5tutuZIYeDU0VDRFWG9IVUE" > phlat-1.0.tar.gz
当我对另一个文件进行同样操作时,
curl -L "https://docs.google.com/uc?export=download&id=0Bz-w5tutuZIYY3h5YlMzTjhnbGM" > index4phlat.tar.gz
我得到以下输出-
我注意到在链接的第三行到最后一行,有一个&confirm=JwkK,这是一个随机的4个字符的字符串,但建议有一种方法添加到我的URL确认。我访问的一个链接建议&confirm=no_antivirus,但这不起作用。
我希望这里有人能帮忙!
ggID='put_googleID_here'
ggURL='https://drive.google.com/uc?export=download'
filename="$(curl -sc /tmp/gcokie "${ggURL}&id=${ggID}" | grep -o '="uc-name.*</span>' | sed 's/.*">//;s/<.a> .*//')"
getcode="$(awk '/_warning_/ {print $NF}' /tmp/gcokie)"
curl -Lb /tmp/gcokie "${ggURL}&confirm=${getcode}&id=${ggID}" -o "${filename}"
它是如何工作的?
使用curl获取cookie文件和html代码。
管道html到grep和sed和搜索文件名。
使用awk从cookie文件中获取确认代码。
最后下载启用cookie的文件,确认代码和文件名。
curl -Lb /tmp/gcokie "https://drive.google.com/uc?export=download&confirm=Uq6r&id=0B5IRsLTwEO6CVXFURmpQZ1Jxc0U" -o "SomeBigFile.zip"
如果你不需要文件名变量卷曲可以猜出来
-L Follow重定向
- o远程名称
- j Remote-header-name
curl -sc /tmp/gcokie "${ggURL}&id=${ggID}" >/dev/null
getcode="$(awk '/_warning_/ {print $NF}' /tmp/gcokie)"
curl -LOJb /tmp/gcokie "${ggURL}&confirm=${getcode}&id=${ggID}"
要从URL提取谷歌文件ID,您可以使用:
echo "gURL" | egrep -o '(\w|-){26,}'
# match more than 26 word characters
OR
echo "gURL" | sed 's/[^A-Za-z0-9_-]/\n/g' | sed -rn '/.{26}/p'
# replace non-word characters with new line,
# print only line with more than 26 word characters
解决方案只使用谷歌驱动器API
在运行下面的代码之前,您必须激活谷歌驱动器API,安装依赖项并验证您的帐户。说明可以在原来的谷歌驱动器API指南页面上找到
import io
import os
import pickle
import sys, argparse
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
from google_auth_oauthlib.flow import InstalledAppFlow
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
def _main(file_id, output):
""" Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
if not file_id:
sys.exit('\nMissing arguments. Correct usage:\ndrive_api_download.py --file_id <file_id> [--output output_name]\n')
elif not output:
output = "./" + file_id
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# Downloads file
request = service.files().get_media(fileId=file_id)
fp = open(output, "wb")
downloader = MediaIoBaseDownload(fp, request)
done = False
while done is False:
status, done = downloader.next_chunk(num_retries=3)
print("Download %d%%." % int(status.progress() * 100))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--file_id')
parser.add_argument('-o', '--output')
args = parser.parse_args()
_main(args.file_id, args.output)
2018年5月
如果你想使用curl从谷歌驱动器下载文件,除了驱动器中的文件id,你还需要谷歌驱动器API的OAuth2 access_token。获取令牌涉及谷歌API框架的几个步骤。谷歌的注册步骤(目前)是免费的。
OAuth2 access_token可能允许所有类型的活动,因此要小心使用它。此外,令牌会在一小段时间后超时(1小时?),但如果有人捕获它,时间还不够短,无法防止滥用。
一旦你有一个access_token和fileid,这将工作:
AUTH="Authorization: Bearer the_access_token_goes_here"
FILEID="fileid_goes_here"
URL=https://www.googleapis.com/drive/v3/files/$FILEID?alt=media
curl --header "$AUTH" $URL >myfile.ext
参见:谷歌驱动器api—REST—下载文件
2022年6月
你可以用gdown。也可以考虑访问该页面以获得完整的说明;这只是一个总结,源回购可能有更多最新的说明。
指令
使用以下命令安装:
pip install gdown
在此之后,您可以通过运行以下命令之一从谷歌驱动器下载任何文件:
gdown https://drive.google.com/uc?id=<file_id> # for files
gdown <file_id> # alternative format
gdown --folder https://drive.google.com/drive/folders/<file_id> # for folders
gdown --folder --id <file_id> # this format works for folders too
示例:从该目录下载自述文件
gdown https://drive.google.com/uc?id=0B7EVK8r0v71pOXBhSUdJWU1MYUk
file_id应该类似于0Bz8a_Dbh9QhbNU3SGlFaDg。您可以通过右键单击感兴趣的文件并选择Get link来找到这个ID。自2021年11月起,该链接的形式为:
# Files
https://drive.google.com/file/d/<file_id>/view?usp=sharing
# Folders
https://drive.google.com/drive/folders/<file_id>
警告
只对开放文件有效。(“任何有链接的人都可以查看”)
不能下载超过50个文件到一个文件夹。
如果您可以访问源文件,您可以考虑使用tar/zip将其变成一个单独的文件来解决这个限制。