I'm trying to download files using python requests. It worked in python 2.7, but not now. I'm really confused and there has to be a simpler answer. Since the files can be quite large, I really want a progressbar, and I'm using python procressbar to do the job.
r = requests.get(file_url, data={'track': 'requests'})
size = int(r.headers['Content-Length'].strip())
self.bytes = 0
widgets = [name, ": ", Bar(marker="|", left="[", right=" "),
Percentage(), " ", FileTransferSpeed(), "] ",
self,
" of {0}MB".format(round(size / 1024 / 1024, 2))]
pbar = ProgressBar(widgets=widgets, maxval=size)
pbar.start()
file = b""
for chunk in r.iter_content()
if chunk:
file += chunk
self.bytes += 1
pbar.update(self.bytes)
I found that using iter_content was the best way to get a continous update. I did try iter_lines but it messed up the files. It stops downloading all of a sudden and is really slow, it takes 15 minutes to download 10% after which it stops. And trying to open a file in byte mode and writing to it doesn't work, it doesn't throw an error at all. And when I try to print what the chunk contains using
print(chunk.decode("utf-8")
Works, but only a few characters. At some point it complains about
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte
Even using "decode_unicode=True" in iter_content does nothing. I'm stumped and don't know what to do. It shouldn't be this hard to use Py3k.