我试图在脚本中从谷歌驱动器下载一个文件,我这样做有点麻烦。我要下载的文件在这里。
我在网上搜了很多,终于下载了其中一个。我得到了文件的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年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%
我无法让Nanoix的perl脚本工作,或者我看到的其他curl示例,所以我开始自己用python研究api。这适用于小文件,但大文件阻塞了可用的ram,所以我找到了一些其他不错的分块代码,使用api的部分下载功能。要点:
https://gist.github.com/csik/c4c90987224150e4a0b2
注意从API接口下载client_secret json文件到本地目录的部分。
源
$ cat gdrive_dl.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
"""API calls to download a very large google drive file. The drive API only allows downloading to ram
(unlike, say, the Requests library's streaming option) so the files has to be partially downloaded
and chunked. Authentication requires a google api key, and a local download of client_secrets.json
Thanks to Radek for the key functions: http://stackoverflow.com/questions/27617258/memoryerror-how-to-download-large-file-via-google-drive-sdk-using-python
"""
def partial(total_byte_len, part_size_limit):
s = []
for p in range(0, total_byte_len, part_size_limit):
last = min(total_byte_len - 1, p + part_size_limit - 1)
s.append([p, last])
return s
def GD_download_file(service, file_id):
drive_file = service.files().get(fileId=file_id).execute()
download_url = drive_file.get('downloadUrl')
total_size = int(drive_file.get('fileSize'))
s = partial(total_size, 100000000) # I'm downloading BIG files, so 100M chunk size is fine for me
title = drive_file.get('title')
originalFilename = drive_file.get('originalFilename')
filename = './' + originalFilename
if download_url:
with open(filename, 'wb') as file:
print "Bytes downloaded: "
for bytes in s:
headers = {"Range" : 'bytes=%s-%s' % (bytes[0], bytes[1])}
resp, content = service._http.request(download_url, headers=headers)
if resp.status == 206 :
file.write(content)
file.flush()
else:
print 'An error occurred: %s' % resp
return None
print str(bytes[1])+"..."
return title, filename
else:
return None
gauth = GoogleAuth()
gauth.CommandLineAuth() #requires cut and paste from a browser
FILE_ID = 'SOMEID' #FileID is the simple file hash, like 0B1NzlxZ5RpdKS0NOS0x0Ym9kR0U
drive = GoogleDrive(gauth)
service = gauth.service
#file = drive.CreateFile({'id':FILE_ID}) # Use this to get file metadata
GD_download_file(service, FILE_ID)
谷歌驱动器的默认行为是扫描文件的病毒,如果文件太大,它将提示用户,并通知他,该文件无法扫描。
目前我找到的唯一解决办法是在网络上共享文件并创建一个网络资源。
引用自谷歌驱动器帮助页面:
使用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