在尝试从Google翻译文本到语音API下载mp3时,我在尝试使用codesc时遇到了错误。代码如下,返回错误的行是codecs.StreamWriter()。
import requests, codecs
def say(text):
response = requests.get('http://translate.google.com/translate_tts?ie=UTF-8&tl=en_us&q='+text)
print(response)
print('Downloading %s...' % (text))
with codecs.StreamWriter(codecs.open('output.mp3', 'wb','utf-8')) as fo:
for chunk in response.iter_content(4096):
fo.write(chunk)
say("Jen hates sharks. Not really.")