Python:使用url从google驱动器下载文件 [英] Python: download files from google drive using url
问题描述
我已经阅读了关于一些drive_service和MedioIO的google api,这也需要一些凭据(主要是json文件/ oauth)。但是我无法了解它的工作原理。
另外,尝试urllib2 urlretrieve,但我的情况是从驱动器获取文件。尝试'wget'也没有用。
试过pydrive库。它有很好的上传功能驱动但没有下载选项。
任何帮助将不胜感激。
谢谢。
如果以drive的url表示可共享链接 Google云端硬盘上的文件,则以下可能有帮助:
导入请求
def download_file_from_google_drive(id ,目的地):
URL =https://docs.google.com/uc?export=download
会话= requests.Session()
response = session.get(URL,params = {'id':id},stream = True)
token = get_confirm_token(response)
如果令牌:
params = { 'id':id,'confirm':token}
response = session.get(URL,params = params,stream = True)
save_response_content(响应,目的地)
def get_confirm_token(response):
为key,value in response.cookies.items():
如果key.startswith('download_warning'):
返回值
return无
def save_response_con帐户(响应,目的地):
CHUNK_SIZE = 32768
with open(destination,wb)as f:
for response.iter_content(CHUNK_SIZE):
if chunk:#filter out keep-alive new chunks
f.write(chunk)
if __name__ ==__main__:
file_id ='TAKE ID FROM SHAREABLE LINK'
destination ='您的磁盘上的DESTINATION FILE'
download_file_from_google_drive(file_id,destination)
虽然剪切的不使用
pydrive ,也不是Google Drive SDK。它使用请求模块(这是以某种方式替代 urllib2 )。
当从Google云端硬盘下载大文件时,单个GET请求是不够的。第二个需要 - 请参阅从谷歌驱动器的wget / curl大文件。
I am trying to download files from google drive and all I have is the drive's url.
I have read about google api that talks about some drive_service and MedioIO, which also requires some credentials( mainly json file/oauth). But I am unable to get any idea about how its working.
Also, tried urllib2 urlretrieve, but my case is to get files from drive. Tried 'wget' too but no use.
Tried pydrive library. It has good upload functions to drive but no download options.
Any help will be appreciated. Thanks.
If by "drive's url" you mean the shareable link of a file on Google Drive, then the following might help:
import requests
def download_file_from_google_drive(id, destination):
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)
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)
if __name__ == "__main__":
file_id = 'TAKE ID FROM SHAREABLE LINK'
destination = 'DESTINATION FILE ON YOUR DISK'
download_file_from_google_drive(file_id, destination)
The snipped does not use pydrive, nor the Google Drive SDK, though. It uses the requests module (which is, somehow, an alternative to urllib2).
When downloading large files from Google Drive, a single GET request is not sufficient. A second one is needed - see wget/curl large file from google drive.
这篇关于Python:使用url从google驱动器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!