马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 鱼cpython学习者 于 2020-9-6 11:00 编辑
想做一个爬取百度翻译的单词音频,然后自动播放音频的爬虫,想来想去觉得用pygame播放音频比较合适,结果pygame打不开我的音频import requests
import json
import os
import pygame
import sys
os.chdir(r'C:\Users\fengu\Desktop\test')
pygame.init()
pygame.mixer.init()
def save_music():
text = input('>>>')
data = {'query': text}
response = requests.post('https://fanyi.baidu.com/langdetect', data = data, headers = headers)
lan = json.loads(response.text)['lan']
url = 'https://fanyi.baidu.com/gettts?lan={0}&text={1}&spd=3&source=web'.format(lan, text)
response = requests.get(url, headers = headers)
with open('music.mp3', 'wb') as f:
f.write(response.content)
save_music()
size = width, height = 1, 1
bg = (255, 255, 255)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == 'QUIT':
sys.exit()
screen.fill(bg)
pygame.display.flip()
test = pygame.mixer.Sound('music.wav')
test.set_volume(0.2)
test.play()
Traceback (most recent call last):
File "C:/MyPythonProjects/WebSpideFile/readword.py", line 33, in <module>
test = pygame.mixer.Sound(r'C:\Users\fengu\Desktop\test\music.wav')
pygame.error: Unable to open file 'C:\\Users\\fengu\\Desktop\\test\\music.wav'
|