How to pass string + web url and get response using python

I use Python and Google Translate. When I click "Play in the broadcast time", created a sound file and URL-address is passed as follows: http://translate.google.com/translate_tts?tl=en&q=text.

I want to create URLs locally using Python, and then send them to the Internet and get these audio files from Google Translate. I tried a lot of URLlib2 and URLparse, but I do not know what I need to do to make this work.

+5
source share
1 answer

You need to change the user agent so that the website thinks you are connecting to the browser. Try the following bit of code:

import urllib2

url = "http://translate.google.com/translate_tts?tl=en&q=text"
request = urllib2.Request(url)
request.add_header('User-agent', 'Mozilla/5.0') 
opener = urllib2.build_opener()

f = open("data.mp3", "wb")
f.write(opener.open(request).read())
f.close()
+6
source

All Articles