Python: download files from google drive using url

前端 未结 9 1379
眼角桃花
眼角桃花 2020-11-27 11:51

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

9条回答
  •  不知归路
    2020-11-27 12:34

    PyDrive allows you to download a file with the function GetContentFile(). You can find the function's documentation here.

    See example below:

    # Initialize GoogleDriveFile instance with file id.
    file_obj = drive.CreateFile({'id': ''})
    file_obj.GetContentFile('cats.png') # Download file as 'cats.png'.
    

    This code assumes that you have an authenticated drive object, the docs on this can be found here and here.

    In the general case this is done like so:

    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)
    

    Info on silent authentication on a server can be found here and involves writing a settings.yaml (example: here) in which you save the authentication details.

提交回复
热议问题