我试图在脚本中从谷歌驱动器下载一个文件,我这样做有点麻烦。我要下载的文件在这里。
我在网上搜了很多,终于下载了其中一个。我得到了文件的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
解决了!
我写了一个从谷歌驱动器下载文件的Python代码片段,给出了一个可共享的链接。截至2017年8月,它是有效的。
剪切不使用gdrive,也没有谷歌驱动器API。它使用请求模块。
当从谷歌驱动器下载大文件时,单个GET请求是不够的。需要第二个URL,这个URL有一个额外的URL参数confirm,它的值应该等于某个cookie的值。
import requests
def download_file_from_google_drive(id, destination):
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params = { 'id' : id }, stream = True)
token = get_confirm_token(response)
if token:
params = { 'id' : id, 'confirm' : token }
response = session.get(URL, params = params, stream = True)
save_response_content(response, destination)
if __name__ == "__main__":
import sys
if len(sys.argv) is not 3:
print("Usage: python google_drive.py drive_file_id destination_file_path")
else:
# TAKE ID FROM SHAREABLE LINK
file_id = sys.argv[1]
# DESTINATION FILE ON YOUR DISK
destination = sys.argv[2]
download_file_from_google_drive(file_id, destination)
替代方法,2020年
适用于无头服务器。我试图下载一个200GB的私人文件,但无法获得任何其他方法,在这个线程中提到,工作。
解决方案
(如果文件已经在您自己的谷歌驱动器中,则跳过此步骤)将想要从公共/共享文件夹下载的文件复制到您的谷歌驱动器帐户中。选择“文件”->右键单击->拷贝
安装Rclone(一个开源命令行工具),在本地存储和谷歌驱动器之间同步文件。这是一个快速教程,安装和设置rclone的谷歌驱动器。
使用Rclone将您的文件从谷歌驱动器复制到您的机器
rclone copy mygoogledrive:path/to/file /path/to/file/on/local/machine -P
-P参数帮助跟踪下载的进度,并让您知道下载何时完成。
获取文件ID:
1.在浏览器中打开谷歌驱动器。
2.右键单击要下载的文件,单击“获取可共享链接”。链接如下所示:https://drive.google.com/file/d/XXX/view?usp=sharing。记录文件ID XXX;你将在下面需要它。
获取一个OAuth令牌:
1.去OAuth 2.0游乐场
2.在“选择和授权API”框中,向下滚动,展开Drive API v3,并选择https://www.googleapis.com/auth/drive.readonly。
3.单击“授权api”,然后为令牌交换授权代码。复制Access令牌YYY;你将在下面需要它。
从命令行下载文件:
如果操作系统为OS X或Linux,打开“终端”程序,输入以下命令。
curl -H "Authorization: Bearer YYY" https://www.googleapis.com/drive/v3/files/XXX?alt=media -o ZZZ
如果使用Windows操作系统,打开PowerShell程序,输入以下命令。
Invoke-RestMethod -Uri https://www.googleapis.com/drive/v3/files/XXX?alt=media -Method Get Headers @{"Authorization"="Bearer YYY"} -OutFile ZZZ
在您的命令中,将XXX替换为上面的文件ID, YYY替换为上面的访问令牌,ZZZ替换为将保存的文件名(例如,如果您下载的是zip文件,则替换为“myFile.zip”)。