可以通过两个不同的URL访问mp3。我正在尝试使用Python来确定哪个URL最快从...下载?
例如,我想计算https://cpx.podbean.com/mf/download/a6bxxa/LAF_15min_044_mindfulness.mp3下载所花费的时间,并将其与http://cpx.podbean.com/mf/play/a6bxxa/LAF_15min_044_mindfulness.mp3下载的时间进行比较。
下载我目前正在使用的mp3: urllib.request.urlretrieve(mp3_url,mp3_filename)
答案 0 :(得分:2)
你基本上可以这样做:
from datetime import datetime
starttime = datetime.now()
urllib.request.urlretrieve(mp3_url, mp3_filename) # Whatever code you're using...
finishtime = datetime.now()
runtime = finishtime - starttime
print str(runtime)
这将打印0:03:19.356798
的时间戳,格式为[小时]:[分钟]:[seconds.micro秒]
我的不好......我没有意识到你正试图找出最快的链接。我不知道你如何存储你的mp3_url
和mp3_filename
元素,但尝试这样的事情(相应调整):
from datetime import datetime
mp3_list = {
'file1.mp3': 'http://www.url1.com',
'file2.mp3': 'http://www.url2.com',
'file3.mp3': 'http://www.url3.com',
}
runtimes = []
for mp3_url, mp3_filename in mp3_list.items(): # i'm not sure how or where you are storing mp3_url or mp3_filename, so you'll have to modify this line accordingly...
starttime = datetime.now()
urllib.request.urlretrieve(mp3_url, mp3_filename) # Whatever code you're using...
finishtime = datetime.now()
runtime = finishtime - starttime
runtimes.append({'runtime': runtime, 'url': mp3_url, 'filename': mp3_filename})
fastest_mp3_url = sorted(runtimes, key=lambda k: k['runtime'])[0]['url']
fastest_mp3_filename = sorted(runtimes, key=lambda k: k['runtime'])[0]['filename']
print fastest_mp3_url
print fastest_mp3_filename