Возможно ли в Python воспроизводить звук / музыку с веб-сайта, не открывая браузер?

#python #selenium #audio #browser #playback

#python #selenium #Аудио #браузер #воспроизведение

Вопрос:

Я нашел какое-то решение с помощью Selenium, но Selenium открывает браузер для воспроизведения выбранной музыки, так что для меня это не идеальный способ.

Возможно ли воспроизводить музыку с веб-сайта, не открывая браузер?

Ответ №1:

 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')
  

Ответ №2:

Вы, вероятно, ищете библиотеку 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)