要通过google translate进行翻译的Python脚本
我正在尝试学习python,所以我决定编写一个脚本,可以使用GoogleTranslate翻译一些东西。到现在为止,我写了这样一封信:要通过google translate进行翻译的Python脚本,python,beautifulsoup,urllib2,google-translate,Python,Beautifulsoup,Urllib2,Google Translate,我正在尝试学习python,所以我决定编写一个脚本,可以使用GoogleTranslate翻译一些东西。到现在为止,我写了这样一封信: import sys from BeautifulSoup import BeautifulSoup import urllib2 import urllib data = {'sl':'en','tl':'it','text':'word'} request = urllib2.Request('http://www.translate.google.co
import sys
from BeautifulSoup import BeautifulSoup
import urllib2
import urllib
data = {'sl':'en','tl':'it','text':'word'}
request = urllib2.Request('http://www.translate.google.com', urllib.urlencode(data))
request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11')
opener = urllib2.build_opener()
feeddata = opener.open(request).read()
#print feeddata
soup = BeautifulSoup(feeddata)
print soup.find('span', id="result_box")
print request.get_method()
现在我被卡住了。我看不到任何bug,但它仍然不起作用(我的意思是脚本将运行,但它不会翻译单词)
有人知道怎么修吗?
(对不起,我的英语很差)谷歌翻译是用来处理
GET
请求而不是POST
请求的。但是,如果您向请求中添加任何数据,urrllib2
将自动提交POST
解决方案是使用查询字符串构造url,这样您将提交GET
您需要更改
request=urllib2.request('http://www.translate.google.com,urllib.urlencode(data))
代码行
下面是:
querystring = urllib.urlencode(data)
request = urllib2.Request('http://www.translate.google.com' + '?' + querystring )
您将获得以下输出:
<span id="result_box" class="short_text">
<span title="word" onmouseover="this.style.backgroundColor='#ebeff9'" onmouseout="this.style.backgroundColor='#fff'">
parola
</span>
</span>
帕罗拉
顺便说一句,你有点违反了谷歌的服务条款;如果你不仅仅是为了训练而编写一个小脚本,那就去看看它们吧
使用请求
我强烈建议您尽可能远离urllib,使用优秀的库,这将使您能够有效地将
HTTP
与Python结合使用 如果您想检查,我制作了这个脚本:
:)是的,他们的文档不容易被发现 以下是您要做的:
import json
from apiclient.discovery import build
query='this is a test to translate english to spanish'
target_language = 'es'
service = build('translate','v2',developerKey='INSERT_YOUR_APP_API_KEY_HERE')
collection = service.translations()
request = collection.list(q=query, target=target_language)
response = request.execute()
response_json = json.dumps(response)
ascii_translation = ((response['translations'][0])['translatedText']).encode('utf-8').decode('ascii', 'ignore')
utf_translation = ((response['translations'][0])['translatedText']).encode('utf-8')
print response
print ascii_translation
print utf_translation
1.1
1.2
1.3
1.4、确保通过IP或其他可用方式限制使用import json
from apiclient.discovery import build
query='this is a test to translate english to spanish'
target_language = 'es'
service = build('translate','v2',developerKey='INSERT_YOUR_APP_API_KEY_HERE')
collection = service.translations()
request = collection.list(q=query, target=target_language)
response = request.execute()
response_json = json.dumps(response)
ascii_translation = ((response['translations'][0])['translatedText']).encode('utf-8').decode('ascii', 'ignore')
utf_translation = ((response['translations'][0])['translatedText']).encode('utf-8')
print response
print ascii_translation
print utf_translation
非常感谢你,它是有效的:)我没有意识到它违反了谷歌的服务条款,只是想学点东西,而且它看起来很有趣。我一定会查看这个请求库,再次感谢:)很高兴我帮了忙。关于术语,考虑一下谷歌翻译现在是有偿服务的事实:如果有什么错误,你会得到什么?正如我所说,我没有任何错误,一切似乎都起作用,但在我得到的:应该有“某事”在这个跨度标签。最后你得到什么?你要求打印出来的方法。你打算返回什么?可能是因为Google Translate有一个功能,如果你想以编程方式翻译文本的话?翻译应该显示在这个span标记中。我本来打算使用BeautifulSoup公开它,但现在我正在尝试获得任何翻译。您可以在代码中添加另一个黑客,在来自fake_useragent import useragent ua=useragent()def getuseragent():的每个请求中伪造用户代理,而True:try:return{'user-agent':ua.random.encode()}除:pass@Arnaud-Aliès你知道translate.google.com的请求限制吗?我正在使用您的模块,不到30分钟,我就收到了
HTTP状态代码429:请求太多。
大多数在线回答都谈到了Translate API的请求限制,但我想知道您是否有Python的请求
模块的数据