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

我在网上搜了很多,终于下载了其中一个。我得到了文件的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栏中抓取文件夹ID。

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

享受:)

其他回答

谷歌驱动器的默认行为是扫描文件的病毒,如果文件太大,它将提示用户,并通知他,该文件无法扫描。

目前我找到的唯一解决办法是在网络上共享文件并创建一个网络资源。

引用自谷歌驱动器帮助页面:

使用Drive,您可以使web资源-如HTML, CSS和Javascript文件-可作为网站查看。

使用Drive托管网页:

Open Drive at drive.google.com and select a file. Click the Share button at the top of the page. Click Advanced in the bottom right corner of the sharing box. Click Change.... Choose On - Public on the web and click Save. Before closing the sharing box, copy the document ID from the URL in the field below "Link to share". The document ID is a string of uppercase and lowercase letters and numbers between slashes in the URL. Share the URL that looks like "www.googledrive.com/host/[doc id] where [doc id] is replaced by the document ID you copied in step 6. Anyone can now view your webpage.

在这里找到:https://support.google.com/drive/answer/2881970?hl=en

例如,当你在谷歌驱动器上公开共享一个文件时,共享链接看起来是这样的:

https://drive.google.com/file/d/0B5IRsLTwEO6CVXFURmpQZ1Jxc0U/view?usp=sharing

然后复制文件id,创建googledrive.com链接,如下所示:

https://www.googledrive.com/host/0B5IRsLTwEO6CVXFURmpQZ1Jxc0U

从谷歌驱动器上下载文件的简单方法,您也可以在colab上下载文件

pip install gdown

import gdown

Then

url = 'https://drive.google.com/uc?id=0B9P1L--7Wd2vU3VUVlFnbTgtS2c'
output = 'spam.txt'
gdown.download(url, output, quiet=False)

or

fileid='0B9P1L7Wd2vU3VUVlFnbTgtS2c'

gdown https://drive.google.com/uc?id=+fileid

文档https://pypi.org/project/gdown/

解决方案只使用谷歌驱动器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)

从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)。

2018年5月工作

嗨,根据这些评论…我创建一个bash导出URL列表从文件URL .txt到URLS_DECODED.txt 在一些加速器如flashget中使用(我使用cygwin来结合Windows和Linux)

引入命令爬行器是为了避免下载并(直接)获得最终链接

命令GREP HEAD和CUT,处理并获得最终链接,是基于西班牙语,也许你可以移植到英语语言

echo -e "$URL_TO_DOWNLOAD\r"可能\r只是cywin,必须用\n(换行符)代替

**********user***********为用户文件夹

*******Localización***********是西班牙语,清除星号,让英语单词定位和适应头部和切割数字适当的方法。

rm -rf /home/**********user***********/URLS_DECODED.txt
COUNTER=0
while read p; do 
    string=$p
    hash="${string#*id=}"
    hash="${hash%&*}"
    hash="${hash#*file/d/}"
    hash="${hash%/*}"
    let COUNTER=COUNTER+1
    echo "Enlace "$COUNTER" id="$hash
    URL_TO_DOWNLOAD=$(wget --spider --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='$hash -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id="$hash 2>&1 | grep *******Localización***********: | head -c-13 | cut -c16-)
    rm -rf /tmp/cookies.txt
    echo -e "$URL_TO_DOWNLOAD\r" >> /home/**********user***********/URLS_DECODED.txt
    echo "Enlace "$COUNTER" URL="$URL_TO_DOWNLOAD
done < /home/**********user***********/URLS.txt