服务器(终端)下载 Google Drive 上面的数据

##  服务器(终端)下载 Google Drive 上面的数据

之前在终端下载google drive上的数据都是先下载到本地,再上传,发现太麻烦了,今天找到一种方法分享给大家,

可以去github上下载下载的python脚本:https://github.com/chentinghao/download_google_drive/blob/master/download_gdrive.py

因为代码比较短,我粘贴过来:


 
 
  1. import requests
  2. from tqdm import tqdm
  3. def download_file_from_google_drive(id, destination):
  4. def get_confirm_token(response):
  5. for key, value in response.cookies.items():
  6. if key.startswith( 'download_warning'):
  7. return value
  8. return None
  9. def save_response_content(response, destination):
  10. CHUNK_SIZE = 32768
  11. with open(destination, "wb") as f:
  12. with tqdm(unit= 'B', unit_scale= True, unit_divisor= 1024) as bar:
  13. for chunk in response.iter_content(CHUNK_SIZE):
  14. if chunk: # filter out keep-alive new chunks
  15. f.write(chunk)
  16. bar.update(CHUNK_SIZE)
  17. URL = "https://docs.google.com/uc?export=download"
  18. session = requests.Session()
  19. response = session.get(URL, params = { 'id' : id }, stream = True)
  20. token = get_confirm_token(response)
  21. if token:
  22. params = { 'id' : id, 'confirm' : token }
  23. response = session.get(URL, params = params, stream = True)
  24. save_response_content(response, destination)
  25. if __name__ == "__main__":
  26. import sys
  27. if len(sys.argv) is not 3:
  28. print( "Usage: python google_drive.py drive_file_id destination_file_path")
  29. else:
  30. # TAKE ID FROM SHAREABLE LINK
  31. file_id = sys.argv[ 1]
  32. # DESTINATION FILE ON YOUR DISK
  33. destination = sys.argv[ 2]
  34. download_file_from_google_drive(file_id, destination)

然后可以直接运行这个脚本;

 

python download_gdrive.py GoogleFileID /path/xxxx.file
 
 

其中GoogleFileID 就是url中的id,/path/xxxx.file就是存放文件的位置和文件名,

获取GoogleFileID如下:

单击右键:

 

点击“获取共享链接”,

复制url,可以得到如下:

https://drive.google.com/open?id=1wp8h_9zzApMskUnQHYsrtvg-nGsQxj0d

那么GoogleFileID就是id后面的那一串,也就是  1wp8h_9zzApMskUnQHYsrtvg-nGsQxj0d

那么运行脚本就是:

python download_gdrive.py 1wp8h_9zzApMskUnQHYsrtvg-nGsQxj0d /path/download.file
 
 
                                </div>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值