如何用Python捕捉mp3流
用Python怎么才能抓取从http流出来的mp3音频,并把它保存到电脑上呢?
到目前为止,我尝试过:
target = open(target_path, "w")
conn = urllib.urlopen(stream_url)
while True:
target.write(conn.read(buf_size))
这样做虽然能得到一些数据,但这些数据要么是乱码,要么在mp3播放器里根本播放不了。
3 个回答
1
也许之前的 urllib 语法有变化(不过那个答案确实让我找到了正确的答案),但这个语法在 Python 3 中是可以用的:
import urllib.request
urllib.request.urlretrieve(stream_url, target_path)
4
最好的方法是:
urllib.urlretrieve(stream_url, target_path);
15
如果你在使用Windows系统,可能会不小心把文件中的换行符转换成CRLF格式,这样会导致二进制数据损坏。试着以二进制模式打开target
文件:
target = open(target_path, "wb")