Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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/16.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
为什么python打印会延迟?_Python_Python 3.x_Web Crawler_Python Requests - Fatal编程技术网

为什么python打印会延迟?

为什么python打印会延迟?,python,python-3.x,web-crawler,python-requests,Python,Python 3.x,Web Crawler,Python Requests,我尝试使用请求下载文件,每次检索100k大小的文件时都打印一个点,但所有的点都在最后打印出来。参见代码 with open(file_name,'wb') as file: print("begin downloading, please wait...") respond_file = requests.get(file_url,stream=True) size = len(respond_file.content)//1000000 #the next l

我尝试使用请求下载文件,每次检索100k大小的文件时都打印一个点,但所有的点都在最后打印出来。参见代码

with open(file_name,'wb') as file:
    print("begin downloading, please wait...")
    respond_file = requests.get(file_url,stream=True)
    size = len(respond_file.content)//1000000

    #the next line will not be printed until file is downloaded
    print("the file size is "+ str(size) +"MB")
    for chunk in respond_file.iter_content(102400):
        file.write(chunk)
        #print('',end='.')
        sys.stdout.write('.')
        sys.stdout.flush()
    print("")

@kevin在评论中写道,
respond.file.content
会阻止执行,直到整个内容下载完毕。我的回答和他的评论之间的唯一区别在于我不是在猜测;)

正如@kevin在评论中所写,
respond.file.content
会阻止执行,直到下载完全部内容。我的回答和他的评论之间的唯一区别在于我不是在猜测;)

这应该是你所期望的。获取respond_文件的长度不是您想要的。而是检查内容长度标题

注意:我将代码改为显示KB(用于测试)


这应该是你所期望的。获取respond_文件的长度不是您想要的。而是检查内容长度标题

注意:我将代码改为显示KB(用于测试)


您正在访问
request.content
此处:

size = len(respond_file.content)//1000000
访问该属性会强制下载整个响应,对于大型响应,这需要一些时间。使用
int(respond_file.headers['content-length'])
代替:

size = int(respond_file.headers['content-length']) // 1000000
Content-Length
标题由服务器提供,由于它是标题的一部分,您可以访问该信息,而无需先下载所有内容

如果服务器选择使用
传输编码:chunked
来流式传输响应,则无需设置
内容长度
头;您可能需要考虑到这一点:

content_length = respond_file.headers.get('content-length', None)
size_in_kb = '{}KB'.format(int(content_length) // 1024) if content_length else 'Unknown'
print("the file size is", size_in_kb)
其中,以KB为单位的大小是通过长度除以1024(而不是一百万)来计算的

或者,在单独的HEAD请求中请求大小(仅获取标题):


您正在访问
request.content
此处:

size = len(respond_file.content)//1000000
访问该属性会强制下载整个响应,对于大型响应,这需要一些时间。使用
int(respond_file.headers['content-length'])
代替:

size = int(respond_file.headers['content-length']) // 1000000
Content-Length
标题由服务器提供,由于它是标题的一部分,您可以访问该信息,而无需先下载所有内容

如果服务器选择使用
传输编码:chunked
来流式传输响应,则无需设置
内容长度
头;您可能需要考虑到这一点:

content_length = respond_file.headers.get('content-length', None)
size_in_kb = '{}KB'.format(int(content_length) // 1024) if content_length else 'Unknown'
print("the file size is", size_in_kb)
其中,以KB为单位的大小是通过长度除以1024(而不是一百万)来计算的

或者,在单独的HEAD请求中请求大小(仅获取标题):



哪些部件延迟?一些
print()
调用或
sys.stdout.write()
调用也可以?胡乱猜测:访问
respond\u file.content
会强制请求在进入下一行之前完整完成。尝试删除
size=…
print(“文件大小是…
行,看看你的点是否以更及时的方式打印出来。@凯文:我错过了,但猜测是正确的。是的。内容导致打印点延迟,可以根据答案解决。延迟了哪些部分?一些
print()
调用或
sys.stdout.write()
调用?胡乱猜测:访问
respond\u file.content
会强制请求完整完成,然后才能继续下一行。请尝试删除
大小=…
打印(“文件大小是…
行,看看你的点是否能以更及时的方式打印出来。@Kevin:我没有注意到,但猜测是正确的。是的。内容会导致打印点延迟,可以根据答案解决。谢谢大家,但当我试着从标题中获取内容长度时,我遇到了以下错误:keyerror,我打印了e响应标题:{'server':'nginx/1.4.2','date':'Tue,05 May 2015 16:18:44 GMT','transfer encoding':'chunked','content type':'text/html','connection':'close'},还有什么办法可以解决吗?@1a1a11a:那么服务器没有提前给你一个长度,你将无法给出有关大小的消息。将更新。下载之前有没有其他方法可以知道文件的大小?@1a1a11a:你可以在URL上使用
HEAD
请求。谢谢大家,但当我尝试获取内容len时从headers中,我得到了以下错误:keyerror,我打印了响应头:{'server':'nginx/1.4.2','date':'Tue,05 May 2015 16:18:44 GMT','transfer encoding':'chunked','content type':'text/html','connection','close'},还有什么办法可以解决吗?@1a1a11a:那么服务器没有提前给你一个长度,你将无法给出有关大小的消息。将更新。下载之前有没有其他方法可以知道文件的大小?@1a1a11a:你可以在URL上使用
HEAD
请求。谢谢大家,但当我尝试获取内容len时从headers中,我得到了以下错误:keyerror,我打印了响应头:{'server':'nginx/1.4.2','date':'Tue,05 May 2015 16:18:44 GMT','transfer encoding':'chunked','content type':'text/html','connection','close'},还有其他方法可以解决吗?谢谢大家,但是当我试图从标题中获取内容长度时,我遇到了以下错误:keyrerror,我打印了响应标题:{'server':'nginx/1.4.2','date':'Tue,05 May 2015 16:18:44 GMT','transfer encoding':'chunked','content type':'text/html','connection':'close',还有其他方法可以解决吗?