捕获来自http的mp3流并使用python将其保存到磁盘的最佳方法是什么?
到目前为止我已经尝试过
target = open(target_path, "w")
conn = urllib.urlopen(stream_url)
while True:
target.write(conn.read(buf_size))
这给了我数据,但它在 mp3 播放器中出现乱码或无法播放。
如果您使用的是 Windows,您可能会不小心进行 CRLF 转换,从而损坏二进制数据。尝试target
以二进制模式打开:
target = open(target_path, "wb")
最好的方法是:
urllib.urlretrieve(stream_url, target_path);
也许语法从以前的 urllib 答案改变了(这让我得到了正确的答案),但这种语法适用于 python3:
import urllib.request
urllib.request.urlretrieve(stream_url, target_path)