python的requests模組進行下載限速
- 2019 年 12 月 24 日
- 筆記
python的requests模組進行下載頻寬限制,進行現在速度限制,避免拉爆伺服器。開啟requests的stream=True就可以進行漸進式下載,然後再適當的sleep一下。就可以減少下載頻寬,限制下載速度了。
# NOTE the stream=True parameter below recvlen = 0 tickss = time.time() with requests.get(mp3url, stream=True) as r: r.raise_for_status() with open(mp3f, 'wb') as f: for chunk in r.iter_content(chunk_size=30720): if chunk: # filter out keep-alive new chunks f.write(chunk) recvlen = recvlen + len(chunk) time.sleep(0.1) # f.flush() tickse = time.time() #f=requests.get(mp3url) #with open(mp3f,"wb") as code: # code.write(f.content) print("------------------------") print(title) print(mp3f) print(mp3url) print("長度:",recvlen) print("耗時:",tickse-tickss,"秒") print("++++++++++++++++++++++++") time.sleep(3)
限速結果日誌如下:
長度: 2950970 耗時: 9.922253608703613 秒 長度: 3413255 耗時: 11.447856426239014 秒 長度: 3465066 耗時: 11.52188777923584 秒 長度: 5089296 耗時: 16.877254486083984 秒 長度: 2969808 耗時: 9.891138792037964 秒 長度: 5777092 耗時: 19.198574781417847 秒 長度: 6185409 耗時: 20.51071047782898 秒