V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Jun 15, 2018 · 1887 views
    This topic created in 2880 days ago, the information mentioned may be changed or developed.

    代码如下

    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 的阈值吗?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2536 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:04 · PVG 13:04 · LAX 22:04 · JFK 01:04
    ♥ Do have faith in what you're doing.