我正在尝试使用请求从我从 txt 文件中读取的 url 列表中下载免费音频。问题是我不确定如何将多个音频 (.mp3) 写入所需的目录。在下面的代码示例中,我最终会得到一个错误,说明我选择了一个目录。如何更改此代码,以便将音频全部保存到选定的目录。作为奖励,能够以自动方式命名正在下载的音频会很好。
我是编程新手,所以我希望我在这里缺乏知识不会打扰你。提前道歉:
with open('text.txt') , 'r') as rf:
with open(os.path.join('/path-to-dir-where-i-want-to-write'),'wb') as wf:
content = rf.readlines()
for line in content:
str1 = ('https://examplewebsite' + line) #generates the full url for the mp3 file
audio = requests.get(str1)
wf.write(audio.content)
import os
if not os.path.exists('/path-to-dir-where-i-want-to-write'):
os.makedirs('/path-to-dir-where-i-want-to-write')
with open('text.txt') , 'r') as rf:
content = rf.readlines()
for line in content:
file_where_i_want_to_save = line
with open(os.path.join('/path-to-dir-where-i-want-to-write', file_where_i_want_to_save),'wb') as wf:
str1 = ('https://examplewebsite' + line) #generates the full url for the mp3 file
audio = requests.get(str1)
wf.write(audio.content)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句