Using requests module to download a file from google drive how to get the content length?
Asked
Active
Viewed 1,146 times
3
-
refer https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url – bigbounty Aug 27 '18 at 17:46
1 Answers
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