V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ft3312591
V2EX  ›  Python

求教用 requests 库写下载脚本问题

  •  
  •   ft3312591 · 2018-06-15 15:34:45 +08:00 · 1164 次点击
    这是一个创建于 2113 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下

    r = requests.get(url,  stream = True,timeout = 10)
    stream_size = r.headers['Content-Length'] #获取下载文件大小
    with open(name + '.mp4', 'wb') as f:
        for chunk in r.iter_content(chunk_size = 1024):
        f.write(chunk) #有时候会卡在这里,即一直在等待下一个 chunk。
    
    

    有时候下载视频,服务器不会返拒绝超时之类的错误(能返回错误倒是好解决),但就是不返回下个 chunk 数据,一直等啊等 所以脚本有时候就卡在那不动了,有办法避免这种情况吗? 获取下个 chunk 值这一段可以加上 timeout 的阈值吗?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5174 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:41 · PVG 17:41 · LAX 02:41 · JFK 05:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.