Python 是否可以在不打开浏览器的情况下从网站播放 sound/music?
Is it possible in Python to play sound/music from website without open the browser?
我找到了一些使用 Selenium 的解决方案,但是 Selenium 打开浏览器播放选定的音乐,所以这对我来说不是完美的方式。
是否可以在不打开浏览器的情况下播放网站上的音乐?
from requests import get
try:
from playsound import playsound
except:
from os import system
system('pip install playsound')
x = get(music_link).content
file = open('music.mp3','wb')
file.write(x)
file.close()
playsound('music.mp3')
您可能正在寻找库 playsound
。
import os
import requests
import playsound
url = 'YOUR_URL_HERE'
downloaded_file_location = '/tmp/audiofile.wav'
# Downloading the audio file
r = requests.get(url)
with open(downloaded_file_location, 'wb') as f:
f.write(r.content)
# Playing the audio file
playsound.playsound(downloaded_file_location, True)
# Removing the audio file
os.remove(downloaded_file_location)
我找到了一些使用 Selenium 的解决方案,但是 Selenium 打开浏览器播放选定的音乐,所以这对我来说不是完美的方式。
是否可以在不打开浏览器的情况下播放网站上的音乐?
from requests import get
try:
from playsound import playsound
except:
from os import system
system('pip install playsound')
x = get(music_link).content
file = open('music.mp3','wb')
file.write(x)
file.close()
playsound('music.mp3')
您可能正在寻找库 playsound
。
import os
import requests
import playsound
url = 'YOUR_URL_HERE'
downloaded_file_location = '/tmp/audiofile.wav'
# Downloading the audio file
r = requests.get(url)
with open(downloaded_file_location, 'wb') as f:
f.write(r.content)
# Playing the audio file
playsound.playsound(downloaded_file_location, True)
# Removing the audio file
os.remove(downloaded_file_location)