I have some problems about the browser response header and the audio files when I use Google Translate TTS API to get the audio information.
My computer system is Windows 10 and I use Python 2.7.12
My test API url is: http://translate.google.com/translate_tts?tl=en&q=student&total=1&idx=0&client=tw-ob
My code is
response = requests.get(tts_url, headers=headers,timeout=2)
print response.content
print response.status_code
The response.status_code
is 200
. However, the response.content
and response.text
are garbled.
When I write the code
print response.encoding
The result is
None
Then I open the Chrome developer tool to view the page's response headers. I do not find the Content-Encoding. The picture is
SO I do not know how to decode response.text
and response.content
At the same time, I write output.write(response.content)
. The output
is a mp3 file. Then I open the mp3 file, but the voice is not a complete pronunciation. I think the file must have some wrong with the encoding.
So I do not know how to deal with this problem and I do not know how to convert the response.text
or response.content
into the correct format to play the mp3 file correctly.