我想从谷歌驱动器下载文件,我所有的是驱动器的网址。Python:使用网址从谷歌驱动器下载文件
我已阅读关于一些drive_service和MedioIO,这也需要一些凭据(主要是json文件/ oauth)的谷歌API。但我无法了解它的工作原理。
另外,尝试urllib2 urlretrieve,但我的情况是从驱动器获取文件。试过'wget',但没用。
试过pydrive库。它有很好的上传功能来驱动,但没有下载选项。
任何帮助将不胜感激。 谢谢。
我想从谷歌驱动器下载文件,我所有的是驱动器的网址。Python:使用网址从谷歌驱动器下载文件
我已阅读关于一些drive_service和MedioIO,这也需要一些凭据(主要是json文件/ oauth)的谷歌API。但我无法了解它的工作原理。
另外,尝试urllib2 urlretrieve,但我的情况是从驱动器获取文件。试过'wget',但没用。
试过pydrive库。它有很好的上传功能来驱动,但没有下载选项。
任何帮助将不胜感激。 谢谢。
PyDrive
允许您使用功能GetContentFile()
下载文件。你可以找到该功能的文档here。
见下面的例子:
# Initialize GoogleDriveFile instance with file id.
file_obj = drive.CreateFile({'id': '<your file ID here>'})
file_obj.GetContentFile('cats.png') # Download file as 'cats.png'.
此代码假定你有一个认证drive
对象,在这个文档可以发现here和here。
在此可以这样判定一般情况下:
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
# Create local webserver which automatically handles authentication.
gauth.LocalWebserverAuth()
# Create GoogleDrive instance with authenticated GoogleAuth instance.
drive = GoogleDrive(gauth)
信息在服务器上的沉默认证可以发现here和需要编写一个settings.yaml
(例如:here),其中保存身份验证信息。
如果“驱动器的网址”你的意思是在谷歌云端硬盘中的文件的分享的链接,那么下面可能会有所帮助:
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,也不是谷歌驱动器但是,SDK。它使用requests模块(这是一种替代urllib2)。
从Google Drive下载大文件时,单个GET请求是不够的。第二个是必要的 - 见wget/curl large file from google drive。
工程,抓好 – United121
这也被如上所述,
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
这将创建它自己的服务器也做认证的脏活
file_obj = drive.CreateFile({'id': '<Put the file ID here>'})
file_obj.GetContentFile('Demo.txt')
此下载文件
有过类似的需求,许多次,我在上面的@ user115202的片段中开始了一个额外的简单类GoogleDriveDownloader
。你可以找到源代码here。
您也可以通过画中画安装:
pip install googledrivedownloader
然后使用很简单,只要:
from google_drive_downloader import GoogleDriveDownloader as gdd
gdd.download_file_from_google_drive(file_id='1iytA1n2z4go3uVCwE__vIKouTKyIDjEq',
dest_path='./data/mnist.zip',
unzip=True)
这段代码会下载在谷歌驱动器共享的归档。在这种情况下,1iytA1n2z4go3uVCwE__vIKouTKyIDjEq
是来自Google云端硬盘的可共享链接的ID。
def download_tracking_file_by_id(file_id, download_dir):
gauth = GoogleAuth(settings_file='../settings.yaml')
# Try to load saved client credentials
gauth.LoadCredentialsFile("../credentials.json")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("../credentials.json")
drive = GoogleDrive(gauth)
logger.debug("Trying to download file_id " + str(file_id))
file6 = drive.CreateFile({'id': file_id})
file6.GetContentFile(download_dir+'mapmob.zip')
zipfile.ZipFile(download_dir + 'test.zip').extractall(UNZIP_DIR)
tracking_data_location = download_dir + 'test.json'
return tracking_data_location
上述函数将给定file_id的文件下载到指定的下载文件夹。现在问题仍然存在,如何获得file_id?只需通过id =拆分url即可获得file_id。
file_id = url.split("id=")[1]
你的回答更有意思 –
第一环断:( – Joe
@Joe固定链接! –