我正在尝试从Google驱动器下载文件,我所拥有的只是驱动器的URL。
我已经阅读了有关API drive_service
和的google API MedioIO
,其中还需要一些凭据(主要是JSON file/OAuth
)。但是我不知道它是如何工作的。
另外,尝试过urllib2.urlretrieve
,但我的情况是从驱动器中获取文件。也尝试wget
过,但没有用。
尝试过的PyDrive
图书馆。它具有良好的驱动上传功能,但没有下载选项。
任何帮助将不胜感激。谢谢。
如果通过“驱动器的网址”表示Google云端硬盘上文件的可共享链接,则以下内容可能会有所帮助:
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)
不过,该片段不使用pydrive,也不使用Google Drive SDK。它使用了请求模块(以某种方式可以替代urllib2)。
从Google云端硬盘下载大文件时,单个GET请求是不够的。需要第二个- 从Google驱动器查看wget / curl大文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句