How to fetch files from Google Drive using Python and a URL?

Hey folks, I’m stuck trying to grab some files from Google Drive. All I’ve got is the URL, and I’m not sure how to proceed. I’ve looked into the Google API stuff, but it’s pretty confusing with all the talk about drive_service and MediaIO. Plus, it needs some kind of JSON credentials or OAuth, which I’m not really getting.

I tried a few other things too:

import urllib.request

# Didn't work for drive files
urllib.request.urlretrieve(url, 'file.txt')

# Wget attempt
import subprocess
subprocess.call(['wget', url])

# PyDrive - good for uploading, not for downloading
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
drive = GoogleDrive(gauth)
# But how to download?

None of these worked out. Any ideas on how to tackle this? I’d really appreciate some help or pointers. Thanks!

I’ve found a straightforward method using the requests library that might solve your problem without diving into the Google API complexities. Here’s a concise approach:

import requests

def download_gdrive_file(file_id, output_path):
    url = f'https://drive.google.com/uc?id={file_id}'
    session = requests.Session()
    response = session.get(url, stream=True)
    
    if 'Content-Disposition' in response.headers:
        with open(output_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
        return True
    return False

# Usage
file_id = 'extract_from_your_url'
download_gdrive_file(file_id, 'output.txt')

This method works for most publicly shared Google Drive files. You’ll need to extract the file ID from your URL (it’s usually the part after ‘/d/’ or ‘/file/d/’). It’s a bit more reliable than gdown for certain file types and sizes.

I’ve encountered this issue before, and I found a solution that might help you out. Instead of wrestling with the Google API, you can use the requests library along with a little URL manipulation. Here’s what worked for me:

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:
                f.write(chunk)

file_id = 'YOUR_FILE_ID_HERE'
destination = 'YOUR_DESTINATION_FILE_PATH'
download_file_from_google_drive(file_id, destination)

Just replace ‘YOUR_FILE_ID_HERE’ with the ID from your Google Drive URL (it’s the long string after ‘/d/’ in the URL) and set your desired destination path. This method bypasses the need for authentication and works for most publicly shared Google Drive files. Hope this helps!

hey samuel, i’ve dealt with this before. try using the ‘gdown’ library. it’s super easy:

pip install gdown

then just:

import gdown
gdown.download(url, ‘output.txt’, quiet=False)

no need for complicated api stuff. works like a charm for most gdrive links!