在Jupyter Notebook用Python下载Dropbox共享文件遇“saving disabled”错误
Hey there, let's figure out how to fix that annoying "saving disabled" error you're hitting when trying to pull a Dropbox shared file into your Jupyter Notebook using urllib.request.urlretrieve. I've dealt with this exact issue before, so here's the breakdown and solutions:
Why This Happens
Dropbox's shared links (even with dl=1 appended) often redirect to a verification page—especially for larger files or if your request looks like it's coming from a script instead of a real browser. The default urllib setup doesn't handle these redirects properly, nor does it mimic a browser's request headers, so Dropbox blocks it with that "saving disabled" message.
Solution 1: Use the requests Library (Most Reliable)
requests handles redirects automatically and makes it easy to add browser-like headers to avoid being blocked. Here's how to do it:
First, install requests if you haven't already (run this in a Jupyter cell):
!pip install requests
Then replace your download function with this:
import os import requests def download_dropbox_file(url): # Extract clean filename (ignoring any URL parameters) filename = url.split("/")[-1].split("?")[0] # Mimic a browser's user agent to avoid being flagged as a script headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' } if not os.path.exists(filename): # Fetch the file with redirects allowed response = requests.get(url, headers=headers, allow_redirects=True) if response.status_code == 200: # Write the content to a file with open(filename, 'wb') as f: f.write(response.content) print(f"Success! {filename} downloaded to your Jupyter environment.") else: print(f"Oops, download failed. Status code: {response.status_code}") print(f"Preview of error content: {response.text[:500]}") # Replace with your actual Dropbox link (make sure it has ?dl=1 at the end) download_dropbox_file('your_shared_file_link?dl=1')
Solution 2: Fix Your urllib Code (If You Must Stick With It)
If you prefer to keep using urllib, you'll need to manually add headers and handle potential redirects. Here's the adjusted code:
import os import urllib.request def download_dropbox_file(url): filename = url.split("/")[-1].split("?")[0] if not os.path.exists(filename): # Add browser-like headers to the request headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' } req = urllib.request.Request(url, headers=headers) try: with urllib.request.urlopen(req) as response: with open(filename, 'wb') as f: f.write(response.read()) print(f"Success! {filename} downloaded.") except urllib.error.HTTPError as e: print(f"HTTP Error: {e.code} - {e.reason}") print(f"Error snippet: {e.read()[:500]}") download_dropbox_file('your_shared_file_link?dl=1')
Quick Checks to Rule Out Simple Issues
- Double-check your Dropbox link: Make sure it's a direct file link (not a folder) and ends with
?dl=1(notdl=0, which opens the preview page). - Verify sharing permissions: Ensure the file is set to allow downloads (Dropbox lets owners restrict downloads, so if you don't own the file, ask the sharer to check this setting).
内容的提问来源于stack exchange,提问作者Shahek

