我正在使用以下内容获取图像,但是下载不正确,只能看到图像的一小部分。文件大小小于256 kB。有什么问题,我该如何解决?
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout = 20) as response:
if response.status == 200:
image = await response.content.read(262144) # 256 kiB
else:
print("Check your URL!")
return
documentation表示read(n)
读取upto n
个字节,但返回的字节可能更少。您必须循环调用它,直到它返回指示EOF的空字符串。
如果您不想实现该循环(仅当您拥有自己的缓冲或流系统时才有用,只需调用read()
而不带size参数即可一次接收所有数据。