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

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

我希望这里有人能帮忙!


当前回答

从2022年3月开始,你可以使用开源的跨平台命令行工具gdrive。与其他解决方案相比,它还可以不受限制地下载文件夹,也可以使用非公共文件。

来源:我从Tobi对另一个答案的评论中发现了gdrive。

当前状态

以前有问题,这个工具没有被谷歌验证,它没有维护。自2021-05-28提交以来,这两个问题都已解决。这也意味着,以前需要谷歌服务帐户的解决方案不再需要。(在极少数情况下,您可能仍会遇到问题;如果是,请尝试ntechp-fork。)

安装gdrive

下载2.1.1二进制文件。选择适合您的操作系统的软件包,例如gdrive_2.1.1 1_linux_amd64.tar.gz。 将其复制到您的路径。 gunzip gdrive_2.1.1_linux_amd64.tar.gz Sudo mkdir /usr/local/bin/gdrive Sudo cp gdrive-linux-amd64 /usr/local/bin/gdrive Sudo chmod a+x /usr/local/bin/gdrive

使用gdrive

Determine the Google Drive file ID. For that, right-click the desired file in the Google Drive website and choose "Get Link …". It will return something like https://drive.google.com/open?id=0B7_OwkDsUIgFWXA1B2FPQfV5S8H. Obtain the string behind the ?id= and copy it to your clipboard. That's the file's ID. Download the file. Of course, use your file's ID instead in the following command. gdrive download 0B7_OwkDsUIgFWXA1B2FPQfV5S8H At first usage, the tool will need to obtain access permissions to the Google Drive API. For that, it will show you a link which you have to visit in a browser, and then you will get a verification code to copy&paste back to the tool. The download then starts automatically. There is no progress indicator, but you can observe the progress in a file manager or second terminal.

额外的技巧:速率限制。要以有限的最大速率下载gdrive(以不淹没本地网络中的上行链路…),您可以使用这样的命令:

gdrive download --stdout 0B7_OwkDsUIgFWXA1B2FPQfV5S8H | \
    pv -br -L 90k | cat > file.ext

pv是PipeViewer。该命令将显示下载的数据量(-b)和下载速率(-r),并将下载速率限制为90kib /s (-L 90k)。

其他回答

有一个开源的多平台客户端,用Go: drive编写。它非常漂亮,功能齐全,而且还在积极开发中。

$ drive help pull
Name
        pull - pulls remote changes from Google Drive
Description
        Downloads content from the remote drive or modifies
         local content to match that on your Google Drive

Note: You can skip checksum verification by passing in flag `-ignore-checksum`

* For usage flags: `drive pull -h`
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 

最简单的方法就是把你想下载的东西放在一个文件夹里。共享该文件夹,然后从URL栏中抓取文件夹ID。

然后进入https://googledrive.com/host/[ID](用你的文件夹ID替换ID) 您应该看到该文件夹中所有文件的列表,单击要下载的文件。一个下载应该然后访问你的下载页面(Ctrl+J在Chrome上),然后你想复制下载链接,然后使用 Wget“下载链接”

享受:)

我写了一个从谷歌驱动器下载文件的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)

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) 您应该将目标文件的权限从“受限”更改为“任何拥有该链接的人”。