开发者

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

开发者 https://www.devze.com 2023-04-01 12:44 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I'm using Python and Google Translate. When I 开发者_高级运维click on Listen while translating, a sound file is generated and a URL is passed like so: http://translate.google.com/translate_tts?tl=en&q=text.

I want to generate URLs locally using Python then send them to the web and receive these sound files from Google Translate. I tried a lot of URLlib2 and URLparse but I have no clue about what I need to do to make this work.


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消