有没有办法从网站下载文件并将其直接上传到 Google 云端硬盘?

Is there a way to download a file from a website and upload it directly to Google Drive?

我正在开发一个 python 脚本,用于从该网站下载一些文件。有没有一种方法可以使用 API?

将它们直接下载并上传到 google 驱动器?

这是下载文件的片段:

def download(): #sólo para fichero de oferta 

from webbot import Browser
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url='www.myweb.com'
web=Browser()
web.go_to(url)

web.click(xpath='/html/body/div[2]/div/div/button')
web.type('myusername', into='idUsuario', id='Usuario')
web.type('password' , into='password' , id='password') #specific selection
web.click(tag='Entrar', xpath='//*[@id="enter-button"]/a') 


time.sleep(3)

web.driver.find_element_by_xpath('//*[@id="NavSeleccionApl"]/ul/li[6]/a').click()

time.sleep(1)
web.driver.find_element_by_xpath('//*[@id="formContent:tablaPlantillasPorPerfil:2:j_id710"]/span').click()

try:
    time.sleep(30)
    web.driver.find_element_by_xpath('//*[@id="formContent:j_id197"]').click()

except:
    print('try again')

这是从我的计算机上传我下载的文件的那个:

def adrive():



import datetime
path=r'C:\Users\myself'

print('Uploading to Drive..')
now = datetime.datetime.now()
file_name=f'file_{"_"+str(now.day) + "_"+str(now.month)+ "_"+str(now.year)}.xlsx'
file_to_up=os.path.join(file_name, path)

headers = {"Authorization": "Bearer myauthcode"} #auth code
para = {
    "name": file_name,
    "parents":["1KPrqoChDo6ZL"] #folder in drive
    }
files = {
    'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
    'file': open(file_to_up, "rb")
    }
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers=headers,
    files=files
    )

print('Subido a Drive')

如果我没记错的话,你已经保存了文件,你只需要上传即可。现在,你已经使用了 google-driver-api 标签,这个答案是如何使用它的:

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaFileUpload

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly','https://www.googleapis.com/auth/drive.file']
def upload_files():
    """
    Creates a folder and upload a file to it
    """
    # authenticate account
    service = get_gdrive_service()
    # folder details we want to make
    folder_metadata = {
        "name": "TestFolder",
        "mimeType": "application/vnd.google-apps.folder"
    }
    # create the folder
    file = service.files().create(body=folder_metadata, fields="id").execute()
    # get the folder id
    folder_id = file.get("id")
    print("Folder ID:", folder_id)
    # upload a file text file
    # first, define file metadata, such as the name and the parent folder ID
    file_metadata = {
        "name": "test.txt",
        "parents": [folder_id]
    }
    # upload
    media = MediaFileUpload("test.txt", resumable=True)
    file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    print("File created, id:", file.get("id"))

您可以访问here for more/detail information. This,对您也有帮助。