
今天在下载 Figshare 数据库的数据时,遇到了一些问题:国内下载速度极慢且频繁断线,超过 100MB 的文件即使挂梯子也很难成功下载。为此,我查阅了一些教程,发现使用 Google Cloud 结合 Google Colab 的方案非常可行。不过在实际操作中,我又遇到了新的问题。经过调整和测试,生成一套可行的下载方案,在此记录并分享给大家。
整体思路是,通过Google Colab在线执行功能,将figshare数据转存至Google云盘,再下载。 主要流程是:



from google.colab import drive
drive.mount('/content/drive')```
import requests
import os
from tqdm import tqdm
# 创建会话(模拟真实浏览器)
session = requests.Session()
session.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://figshare.com/'
})
figshare_url = "xxxxxx替换自己的下载链接"
# 保存路径(自定义)
save_dir = "/content/drive/MyDrive/Figshare/UCE"
os.makedirs(save_dir, exist_ok=True)
save_path = os.path.join(save_dir, "文件名.rds")
print("开始下载...")
print(f"目标: {save_path}\n")
try:
# 关键:让 requests 自动跟随所有重定向
with session.get(
figshare_url,
stream=True,
allow_redirects=True, # 自动跟随重定向
timeout=120
) as response:
response.raise_for_status()
# 显示最终 URL
print(f"最终 URL: {response.url[:100]}...")
# 获取文件信息
total_size = int(response.headers.get('content-length', 0))
content_type = response.headers.get('content-type', 'unknown')
print(f"Content-Type: {content_type}")
print(f"大小: {total_size / (1024*1024):.2f} MB\n")
# 如果返回 HTML 且很小,说明不是真实文件
if'html'in content_type.lower() and total_size < 50000:
print("❌ 返回的是网页,不是文件")
print(f"内容预览:\n{response.text[:300]}")
raise Exception("未获取到文件下载链接")
# 开始下载
print("正在下载...")
with open(save_path, 'wb') as f, tqdm(
total=total_size,
unit='iB',
unit_scale=True,
unit_divisor=1024
) as bar:
for chunk in response.iter_content(chunk_size=32768):
if chunk:
f.write(chunk)
bar.update(len(chunk))
# 验证
actual_size = os.path.getsize(save_path)
print(f"\n{'='*60}")
if actual_size > 100000:
print(f"✅ 成功!文件大小: {actual_size/(1024*1024):.2f} MB")
else:
print(f"❌ 失败!文件只有 {actual_size} 字节")
print(f"位置: {save_path}")
print(f"{'='*60}")
except Exception as e:
print(f"\n❌ 错误: {e}")
# 查看结果
!ls -lh /content/drive/MyDrive/Figshare/UCE/
运行完成结果如下:

