最近有个 TTS 需求,于是便想到了 Google TTS.然而网上找到的调用方式都返回 403 503 等等,完全没有头绪.

然后在一个 Gist 里面找到了能用的.

wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q=Test&tl=En-gb"

可以看到 UA 需要设置为 Mozilla 防止验证码,文字是在 q= 后面,tl= 则是语言.

那么就很简单了.

#!/usr/bin/env python3
import requests
headers = {'user-agent': 'Mozilla/5.0'}
url = "http://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q=Test&tl=En-gb"
r = requests.get(url, headers=headers, stream=True)
voice = r.raw.read()

然后直接操作 voice 即可.


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.