3

Using requests module to download a file from google drive how to get the content length?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Yashik
  • 391
  • 5
  • 17

1 Answers1

4

I got the content-length with the use of range

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]
Yashik
  • 391
  • 5
  • 17
  • And why not split the Content-Range header on the slash? (`contlength = int(range.partition('/')[-1])`) – Martijn Pieters Aug 27 '18 at 18:05
  • I just found myself a file big enough to trigger the transition from direct serve to a chunked transfer-encode and no content-length header. The range request is an interesting trick to use! It is rather specific to servers that support HTTP range requests, but something I'll keep in mind. – Martijn Pieters Aug 27 '18 at 18:08