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

我在网上搜了很多,终于下载了其中一个。我得到了文件的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年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) 您应该将目标文件的权限从“受限”更改为“任何拥有该链接的人”。

其他回答

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

有一个开源的多平台客户端,用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`

获取文件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”)。

截至2018年3月更新。

我尝试了其他答案中给出的各种技术,直接从谷歌驱动器下载我的文件(6 GB)到我的AWS ec2实例,但没有一个有效(可能是因为它们太旧了)。

所以,为了让其他人知道,下面是我是如何成功做到的:

Right-click on the file you want to download, click share, under link sharing section, select "anyone with this link can edit". Copy the link. It should be in this format: https://drive.google.com/file/d/FILEIDENTIFIER/view?usp=sharing Copy the FILEIDENTIFIER portion from the link. Copy the below script to a file. It uses curl and processes the cookie to automate the downloading of the file. #!/bin/bash fileid="FILEIDENTIFIER" filename="FILENAME" curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename} As shown above, paste the FILEIDENTIFIER in the script. Remember to keep the double quotes! Provide a name for the file in place of FILENAME. Remember to keep the double quotes and also include the extension in FILENAME (for example, myfile.zip). Now, save the file and make the file executable by running this command in terminal sudo chmod +x download-gdrive.sh. Run the script using `./download-gdrive.sh".

PS:这是上面给出的脚本的Github要点:https://gist.github.com/amit-chahar/db49ce64f46367325293e4cce13d2424

有一个更简单的方法。

从firefox/chrome扩展安装cliget/CURLWGET。

从浏览器下载文件。这将创建一个curl/wget链接,用于记住下载文件时使用的cookie和头文件。使用此命令从任何shell下载