我试图在脚本中从谷歌驱动器下载一个文件,我这样做有点麻烦。我要下载的文件在这里。

我在网上搜了很多,终于下载了其中一个。我得到了文件的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,但这不起作用。

我希望这里有人能帮忙!


当前回答

你可以从谷歌得到url下载链接…/file/d/FILEID/view?usp=share_link并提取FILEID部分。然后在下面替换它(它在那里两次)。

wget --load-cookies /tmp/cookies.txt \
     "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID')" -O- \
    | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=FILEID" -O FILENAME && \
    rm -rf /tmp/cookies.txt

将FILENAME替换为上面一行中应该调用的文件并享受。

其他回答

我用python脚本和谷歌驱动器api做到了这一点, 您可以尝试以下片段:

//using chunk download

file_id = 'someid'
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)

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将其变成一个单独的文件来解决这个限制。

2020年7月- Windows用户批处理文件解决方案

我想为windows用户添加一个简单的批处理文件解决方案,因为我只发现了linux解决方案,我花了几天时间来学习为windows创建解决方案的所有这些东西。因此,为了避免其他人可能需要它,这里是。

你需要的工具

wget for windows (5KB exe小程序,无需安装) 从这里下载。 https://eternallybored.org/misc/wget/ jrepl for windows (117KB的批处理程序,无需安装) 该工具类似于linux的sed工具。 从这里下载: https://www.dostips.com/forum/viewtopic.php?t=6044

假设

%filename% -你想下载的文件将被保存到的文件名。 %fileid% =谷歌文件id(前面已经解释过了)

批量代码下载小文件从谷歌驱动器

wget -O "%filename%" "https://docs.google.com/uc?export=download&id=%fileid%"        

批量代码下载大文件从谷歌驱动器

set cookieFile="cookie.txt"
set confirmFile="confirm.txt"
   
REM downlaod cooky and message with request for confirmation
wget --quiet --save-cookies "%cookieFile%" --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=%fileid%" -O "%confirmFile%"
   
REM extract confirmation key from message saved in confirm file and keep in variable resVar
jrepl ".*confirm=([0-9A-Za-z_]+).*" "$1" /F "%confirmFile%" /A /rtn resVar
   
REM when jrepl writes to variable, it adds carriage return (CR) (0x0D) and a line feed (LF) (0x0A), so remove these two last characters
set confirmKey=%resVar:~0,-2%
   
REM download the file using cookie and confirmation key
wget --load-cookies "%cookieFile%" -O "%filename%" "https://docs.google.com/uc?export=download&id=%fileid%&confirm=%confirmKey%"
   
REM clear temporary files 
del %cookieFile%
del %confirmFile%

我使用这个小脚本,只得到从谷歌驱动器复制的URL:

#!/bin/bash

name=`curl $1 |  grep -w \"name\" | sed 's/.*"name" content="//' | 
sed 's/".*//'`
id=`echo $1 | sed 's#.*/d/##; s#/view.*##'`
curl -L https://drive.google.com/uc?id=$id > $name
# or
# wget -O $name https://drive.google.com/uc?id=$id

2022年4月

首先,从谷歌驱动器中提取所需文件的ID: 在浏览器中,导航到drive.google.com。 右键单击文件,点击“获取可共享链接” 然后从URL中提取文件的ID: 接下来,使用pip安装gdown PyPI模块: PIP安装gdown 最后,使用gdown和预期的ID下载文件: gdown——id <put-the-ID>


【注意】:

在google-colab你必须使用!在bash命令之前。 (即!gdown——id 1-1wAx7b-USG0eQwIBVwVDUl3K1_1ReCt) 您应该将目标文件的权限从“受限”更改为“任何拥有该链接的人”。