gpt4 book ai didi

Python:使用 url 从谷歌驱动器下载文件

转载 作者:IT老高 更新时间:2023-10-28 20:37:19 27 4
gpt4 key购买 nike

我正在尝试从谷歌驱动器下载文件,而我只有驱动器的 URL。

我已经阅读了关于 google API 的内容,其中谈到了一些 drive_serviceMedioIO,这也需要一些凭据(主要是 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。它使用 requests模块(不知何故,它是 urllib2 的替代品)。

从 Google Drive 下载大文件时,单个 GET 请求是不够的。需要第二个 - 见 wget/curl large file from google drive .

关于Python:使用 url 从谷歌驱动器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38511444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com