【问题标题】:Python script to translate via google translatePython脚本通过谷歌翻译进行翻译
【发布时间】:2012-03-13 08:40:11
【问题描述】:

我正在尝试学习 python,所以我决定编写一个可以使用 google translate 翻译某些内容的脚本。直到现在我写了这个:

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

现在我被困住了。我看不到它有任何错误,但它仍然不起作用(我的意思是脚本会运行,但它不会翻译单词)。

有人知道怎么解决吗? (对不起我的英语不好)

【问题讨论】:

  • 如果有什么错误?
  • 正如我所说,我没有收到任何错误,一切似乎都正常,但我得到:在这个跨度标签中应该是“某事”。
  • 你到底得到了什么?你要求打印出方法。你打算返回什么?
  • 如果你想以编程方式翻译文本,也许是因为谷歌翻译有an API you should use
  • 翻译应该显示在这个 span 标签中。我打算使用 BeautifulSoup 来公开它,但现在我正在尝试进行任何翻译。

标签: python beautifulsoup urllib2 google-translate


【解决方案1】:

如果你想检查它,我制作了这个脚本: https://github.com/mouuff/Google-Translate-API :)

【讨论】:

  • 您可以在代码中添加另一个 hack,以在 fake_useragent 的每个请求中伪造用户代理 import UserAgent ua = UserAgent() def getuseragent(): while True: try: return {'User-Agent' : ua.random.encode()} 除外:通过
  • @Arnaud-Aliès 你知道 translate.google.com 的请求限制吗?我正在使用你的模块,不到 30 分钟我收到了HTTP Status code 429: too many requests. 大多数在线答案都谈到了 Translate API 的请求限制,但我想知道你是否有 Python 的 requests 模块的数据
【解决方案2】:

Google 翻译旨在用于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>

顺便说一句,你有点违反了 Google 的服务条款;如果您所做的不仅仅是编写一个用于训练的小脚本,请研究它们。

使用requests

我强烈建议您尽可能远离 urllib,并使用出色的 requests 库,这将使您能够有效地将 HTTP 与 Python 一起使用。

【讨论】:

  • 非常感谢,它有效:) 我没有意识到这违反了谷歌的服务条款,只是想学习一些东西,而且看起来很有趣。我一定会检查这个请求库,再次感谢:)
  • 很高兴我能帮上忙。关于条款,考虑一下谷歌翻译现在是一项付费服务​​这一事实:code.google.com/intl/fr-FR/apis/language/translate/v2/…
【解决方案3】:

是的,他们的文档不是那么容易被发现的。

这是你要做的:

  1. 在谷歌云平台控制台中

    1.1Go to the Projects page and select or create a new project

    1.2Enable billing for your project

    1.3Enable the Cloud Translation API

    1.4 Create a new API key in your project,请确保通过 IP 或那里可用的其他方式限制使用。


  1. 在您要运行客户端的机器上

    pip install --upgrade google-api-python-client


  1. 然后你可以写这个来发送翻译请求和接收响应:

这是代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多