You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Heroku中使用Selenium下载文件至Google Drive及路径配置报错问题

在Heroku环境中使用Selenium将文件下载至Google Drive的解决方案

一、直接用Google Drive URL作为download_path报错的原因

download_path参数要求的是本地文件系统的绝对路径,Google Drive文件夹的URL是远程网络资源地址,Selenium的浏览器下载配置无法直接将文件写入远程地址,因此会触发“文件未找到”错误。

二、正确实现流程

要实现目标,需分两步:先让Selenium把文件下载到Heroku的临时本地存储,再通过Google Drive API将本地文件上传到指定Drive文件夹。

步骤1:配置Selenium在Heroku的本地下载路径

Heroku提供临时文件系统,/tmp是所有dyno可访问的临时存储目录,以此作为Selenium的下载路径:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# Heroku环境下必须的无头模式及性能优化参数
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")

# 配置Chrome下载行为
prefs = {
    "download.default_directory": "/tmp",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True
}
chrome_options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(options=chrome_options)

步骤2:通过Google Drive API上传本地文件到指定文件夹

  1. 前置准备:在Google Cloud Console创建项目,启用Google Drive API,生成服务账号密钥(JSON格式),将密钥内容添加到Heroku的环境变量(比如命名为GOOGLE_DRIVE_SERVICE_ACCOUNT)。
  2. 安装依赖:在requirements.txt中添加以下包:
    google-api-python-client
    google-auth-httplib2
    google-auth-oauthlib
    selenium
    
  3. 上传代码实现:
import os
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

# 配置Drive API权限
SCOPES = ["https://www.googleapis.com/auth/drive.file"]
creds = Credentials.from_service_account_info(
    os.environ.get("GOOGLE_DRIVE_SERVICE_ACCOUNT"),
    scopes=SCOPES
)
drive_service = build("drive", "v3", credentials=creds)

def upload_to_drive(local_file_path, drive_folder_id):
    # 构建文件元数据,指定上传到目标文件夹
    file_metadata = {
        "name": os.path.basename(local_file_path),
        "parents": [drive_folder_id]
    }
    media = MediaFileUpload(local_file_path, resumable=True)
    # 执行上传
    file = drive_service.files().create(
        body=file_metadata,
        media_body=media,
        fields="id"
    ).execute()
    print(f"文件上传完成,Drive文件ID: {file.get('id')}")

# 调用示例:将临时目录下的下载文件上传到目标Drive文件夹
downloaded_file_path = "/tmp/your_downloaded_file.ext"
target_drive_folder_id = "1IPNuefeIXXCKm8Xxm3u1ekltxTF3dCT0"  # 从你的Drive文件夹URL中提取的ID
upload_to_drive(downloaded_file_path, target_drive_folder_id)

步骤3:Heroku环境的关键注意事项

  • Heroku的/tmp目录是临时存储,dyno重启后会清空,所以文件下载完成后要立即执行上传操作,避免文件丢失。
  • 需为Heroku应用添加两个buildpack:heroku-buildpack-chromedriver和heroku-buildpack-google-chrome,确保环境中存在Chrome浏览器和ChromeDriver。
  • 服务账号密钥必须通过Heroku环境变量管理,禁止硬编码到代码中,防止密钥泄露。

补充修正

你之前的代码mudopy.download_path(r"https://drive.google.com/drive/folders/1IPNuefeIXXCKm8Xxm3u1ekltxTF3dCT0"),只需将URL替换为Heroku临时路径/tmp,即可解决本地下载路径的错误,再配合上述Drive API代码完成最终的上传需求。

内容的提问来源于stack exchange,提问作者Nkosi Ncube

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 16:41:08