从谷歌共享驱动器下载文件

时间:2020-12-28 08:59:25

标签: python google-apps-script google-drive-api google-oauth google-api-python-client

我想将文件 - Sarcasm_Headlines_Dataset.json 和 glove.6B.300d.txt 从 https://drive.google.com/drive/folders/1s37-DVvRWfOcv59ojc6DnwHzPFkA6e8M 下载到我的驱动器中,以便我可以在 Google 协作中阅读。

在 google collab 中,我尝试了以下操作:

import urllib.request
urllib.request.urlretrieve('https://drive.google.com/drive/folders/1s37-DVvRWfOcv59ojc6DnwHzPFkA6e8M', 'Sarcasm_Headlines_Dataset')
os.listdir() 

import pandas as pd
url = 'https://drive.google.com/drive/folders/1s37-DVvRWfOcv59ojc6DnwHzPFkA6e8M'
df = pd.read_json(url, orient='columns')

https://developers.google.com/drive/api/v3/quickstart/python - a) 我将用于项目快速入门的桌面应用程序的客户端 API 下载到我的本地驱动器中。 (credentials.json)。 b) 安装了谷歌客户端库 c) 当我运行 python quickstart.py 时,它说请访问这个 URL 来授权这个应用程序。我访问了网址并得到了

Error 400: redirect_uri_mismatch; does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

我转到 API 控制台并在 Web 浏览器中获取了客户端 ID 和客户端密钥。 再次重新运行 python quickstart.py 并得到相同的错误。

我相信我需要在 google collab 中运行以下代码:

request = service.files().get_media(fileId=file_id)
fh = io.FileIO(location + filename, 'wb')
downloader = MediaIoBaseDownload(fh, request, 1024 * 1024 * 1024)

我无法使用 file_id。请帮忙。

我希望 json 在 google collab 中运行 LSTM 代码。

1 个答案:

答案 0 :(得分:0)

你甚至不需要 API 哈哈

您可以通过右键单击文件并单击查看链接来获取文件 ID,从那里复制链接并删除除 ID 之外的所有内容,如下所示 https://drive.google.com/file/d/**1LWVnOp1rw4g9SYswuf8IPAHBoOnWOTF0**/view?usp=sharing

import requests
# Getting the File id
file1_id = '1-JzJ5MIVuKG6Vhg5tA1ATh185WmXUqWL'
file2_id = '1LWVnOp1rw4g9SYswuf8IPAHBoOnWOTF0'

# Requesting data
file1 = requests.get('https://drive.google.com/uc?export=download&confirm=9_s_&id=' + file1_id)
file2 = requests.get('https://drive.google.com/uc?export=download&confirm=9_s_&id=' + file2_id)

# Saving data
open('glove.6B.300d.txt', 'wb').write(file1.content)
open('Sarcasm_Headlines_Dataset.json', 'wb').write(file2.content)