【问题标题】:How to get content length for google drive download ?如何获取谷歌驱动器下载的内容长度?
【发布时间】:2018-08-27 17:44:18
【问题描述】:

使用 requests 模块从 google drive 下载文件如何获取内容长度?

【问题讨论】:

标签: python python-3.x python-requests


【解决方案1】:

我使用范围得到了内容长度

import re,requests

URL = "https://docs.google.com/uc?export=download"
headers = {'Range':'bytes=0-'}

r = requests.get(URL,headers=headers,stream=True).headers['Content-Range']

#contleng=int(re.split('\W+',r))[-1]
contleng=int(r.partition('/')[-1]) #Thanks to Maritijn Pieters
contrange=int(re.split('\W+',r))[-2]

【讨论】:

  • 为什么不在斜杠上拆分 Content-Range 标头? (contlength = int(range.partition('/')[-1]))
  • 我刚刚发现自己的文件足够大,可以触发从直接服务到分块传输编码的转换,并且没有内容长度标头。范围请求是一个有趣的技巧!它是特定于支持 HTTP 范围请求的服务器,但我会记住这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多