【发布时间】: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