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

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

我希望这里有人能帮忙!


当前回答

替代方法,2020年

适用于无头服务器。我试图下载一个200GB的私人文件,但无法获得任何其他方法,在这个线程中提到,工作。

解决方案

(如果文件已经在您自己的谷歌驱动器中,则跳过此步骤)将想要从公共/共享文件夹下载的文件复制到您的谷歌驱动器帐户中。选择“文件”->右键单击->拷贝

安装Rclone(一个开源命令行工具),在本地存储和谷歌驱动器之间同步文件。这是一个快速教程,安装和设置rclone的谷歌驱动器。 使用Rclone将您的文件从谷歌驱动器复制到您的机器

rclone copy mygoogledrive:path/to/file /path/to/file/on/local/machine -P

-P参数帮助跟踪下载的进度,并让您知道下载何时完成。

其他回答

谷歌硬盘也有同样的问题。

下面是我如何使用Links 2解决这个问题。

Open a browser on your PC, navigate to your file in Google Drive. Give your file a public link. Copy the public link to your clipboard (eg right click, Copy link address) Open a Terminal. If you're downloading to another PC/server/machine you should SSH to it as this point Install Links 2 (debian/ubuntu method, use your distro or OS equivalent) sudo apt-get install links2 Paste the link in to your terminal and open it with Links like so: links2 "paste url here" Navigate to the download link within Links using your Arrow Keys and press Enter Choose a filename and it'll download your file

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

在弄了这些垃圾之后。我找到了一种方法来下载我的甜蜜文件使用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

解决了!

最简单的方法是:

创建下载链接并复制文件id 用WGET下载: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

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%