我试图在脚本中从谷歌驱动器下载一个文件,我这样做有点麻烦。我要下载的文件在这里。
我在网上搜了很多,终于下载了其中一个。我得到了文件的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,但这不起作用。
我希望这里有人能帮忙!
在弄了这些垃圾之后。我找到了一种方法来下载我的甜蜜文件使用chrome开发工具。
At your google docs tab, Ctr+Shift+J (Setting --> Developer tools)
Switch to Network tabs
At your docs file, click "Download" --> Download as CSV, xlsx,....
It will show you the request in the "Network" console
Right click -> Copy -> Copy as Curl
Your Curl command will be like this, and add -o to create a exported file.
curl 'https://docs.google.com/spreadsheets/d/1Cjsryejgn29BDiInOrGZWvg/export?format=xlsx&id=1Cjsryejgn29BDiInOrGZWvg' -H 'authority: docs.google.com' -H 'upgrade-insecure-requests: 1' -H 'user-agent: Mozilla/5.0 (X..... -o server.xlsx
解决了!
解决方案只使用谷歌驱动器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)
这里有一个快速的方法。
确保链接是共享的,它看起来会像这样:
https://drive.google.com/open?id=FILEID&authuser=0
然后,复制该FILEID并像这样使用它
wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O FILENAME
如果文件很大并且触发了病毒检查页面,您可以使用这样做(但它会下载两个文件,一个html文件和实际文件):
wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -r -A 'uc*' -e robots=off -nd
Skicka是一个cli工具,用于从谷歌硬盘上传、下载访问文件。
的例子,
skicka download /Pictures/2014 ~/Pictures.copy/2014
10 / 10 [=====================================================] 100.00 %
skicka: preparation time 1s, sync time 6s
skicka: updated 0 Drive files, 10 local files
skicka: 0 B read from disk, 16.18 MiB written to disk
skicka: 0 B uploaded (0 B/s), 16.18 MiB downloaded (2.33 MiB/s)
skicka: 50.23 MiB peak memory used
简单的方法:
(如果你只需要一次性下载)
去谷歌驱动器的网页,有下载链接
打开浏览器控制台,转到“网络”选项卡
点击下载链接
等待它的文件开始下载,并找到相应的请求(应该是列表中的最后一个),然后可以取消下载
右键单击请求并单击“复制为cURL”(或类似的)
你应该得到如下内容:
curl 'https://doc-0s-80-docs.googleusercontent.com/docs/securesc/aa51s66fhf9273i....................blah blah blah...............gEIqZ3KAQ==' --compressed
在控制台中,添加> my-file-name。扩展到最后(否则它会把文件写到你的控制台),然后按enter:)
这个链接确实有某种过期时间,所以在生成第一个请求后几分钟就不能开始下载了。