gpt4 book ai didi

python - 收到特定数量的字节后停止下载?

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:51 24 4
gpt4 key购买 nike

有没有办法在收到特定字节后停止从 URL 下载?

在 PHP 中有:

$contents = @file_get_contents($page, FALSE, NULL, 0, 40000);

第五个参数告诉 file_get_contents 在 40000 字节后停止下载。我基本上是在 Python 中寻找类似的东西。在谷歌上搜索和阅读文档没有产生任何结果。帮助会很棒,我是 Python 的新手。

谢谢。

最佳答案

网址库

如果您正在使用 urllib.urlopen:

>>> import urllib
>>> u = urllib.urlopen('http://stackoverflow.com')
>>> x = u.read(1000)
>>> len(x)
1000
>>> u.close()

urllib.urlopen 返回类文件对象;您可以指定要下载的字节数。

请求

>>> import requests 
>>> r = requests.get('http://stackoverflow.com', stream=True)
>>> x = next(r.iter_content(1000), '')[:1000] # iter_content() could yield more than requested; need [:1000]
>>> len(x)
1000

关于python - 收到特定数量的字节后停止下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18330952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com