How to pass a string + web url and receive the response using python [closed]

大城市里の小女人 提交于 2019-12-05 11:39:53

You need to change the user-agent so that the website thinks you are connecting with a 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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!