0

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

Response Headers

Request Headers

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.

  • My computer system is Windows 10 and I use Python 2.7.12 to code. My test API url is [link](http://translate.google.com/translate_tts?tl=en&q=test&total=1&idx=0&client=tw-ob) – RZdiversity Aug 11 '16 at 11:44
  • I thought this was blocked unless you're using a hack http://stackoverflow.com/q/32053442/1256219 – brandall Aug 12 '16 at 00:42

1 Answers1

0

the payload of the response is a binary file, therefore make sure you write the content as binary data and not as text.

Guy Rotem
  • 151
  • 2
  • 6