Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3和带有progressbar的请求_Python_Python 3.x_Request_Progress Bar_Chunks - Fatal编程技术网

Python3和带有progressbar的请求

Python3和带有progressbar的请求,python,python-3.x,request,progress-bar,chunks,Python,Python 3.x,Request,Progress Bar,Chunks,我正在尝试使用python请求下载文件。它在Python2.7中工作,但现在不行。我真的很困惑,必须有一个更简单的答案。由于文件可能相当大,我真的需要一个progressbar,我正在使用python progressbar来完成这项工作 r = requests.get(file_url, data={'track': 'requests'}) size = int(r.headers['Content-Length'].stri

我正在尝试使用python请求下载文件。它在Python2.7中工作,但现在不行。我真的很困惑,必须有一个更简单的答案。由于文件可能相当大,我真的需要一个progressbar,我正在使用python progressbar来完成这项工作

                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)
我发现使用iter_内容是获得持续更新的最佳方式。我确实试过iter_线,但它把文件弄乱了。它突然停止下载,速度非常慢,下载10%需要15分钟,然后停止。试图以字节模式打开一个文件并写入它是不起作用的,它根本不会抛出错误。当我试着用

print(chunk.decode("utf-8")
可以,但只有几个字符。在某种程度上,它抱怨

UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

即使在iter_内容中使用“decode_unicode=True”也没有任何作用。我被难住了,不知道该怎么办。使用Py3k应该不会这么难。

设法解决了它。下面是更新后的代码:

r = requests.get(file_url)
size = int(r.headers['Content-Length'].strip())
self.bytes = 0
widgets = [name, ": ", Bar(marker="|", left="[", right=" "),
    Percentage(), " ",  FileTransferSpeed(), "] ",
    self,
    " of {0}MB".format(str(round(size / 1024 / 1024, 2))[:4])]
pbar = ProgressBar(widgets=widgets, maxval=size).start()
file = []
for buf in r.iter_content(1024):
    if buf:
        file.append(buf)
        self.bytes += len(buf)
        pbar.update(self.bytes)
pbar.finish()

下载速度从7kb/s更改为400+kb/s。它完全可以工作。

这不是一个将其写入文件的进度条吗,因为在执行requests.get()调用时请求已完成?requests.get()函数返回一个响应对象。除非你用它做点什么(比如r=requests.get(file\u url);r.text),否则它实际上什么都做不了。我在下载时对内容进行了修改。事实上,我后来不得不将其保存到一个文件:)@thabubble实际上,您需要调用
requests.get(file\u url,prefetch=False)
才能使您的语句为真。否则,@antihero完全正确。那么,我不知道为什么它会起作用。但确实如此。据我所知,我所做的是“r=self.s.get(file_url,data={'track':'requests'})”,然后使用“r.iter_content()中的for char:”对其进行调整,它确实有效。你可以在下载时看到它。但这是很久以前的事了,我可能有错误的文件。但在我发布我的答案后,它被编辑得很好。我记得它只会下载标题,而不是正文。为此,您必须使用r.text或r.iter_content()。