1

I writing a python script (personal use only) to translate a text using the terminal on linux.

I tried:

#!/usr/bin/python
import urllib,urllib2

url="http://translate.google.com/#en|pt|love"
#url="http://www.google.com"
request=urllib2.Request(url)
answer=urllib2.urlopen(request).read()

print "request:\n",request,"\n\n"
print "answer:\n",answer,"\n\n"

But it isn't working. The truth if we uncomment the line #url="http://www.google.com" and comment url="http://translate.google.com/#en|pt|love" all works.

It strange because if we copy url="http://translate.google.com/#en|pt|love" and paste in the browser the url will works.

So, how can I fix this to catch the results?

4

2 に答える 2

3

URL には、en|pt|love文字列がフラグメントとして含まれています ( の後#)。フラグメントは、クライアント側 (JavaScript) の処理を​​目的としています。サーバーには送信されません。言い換えれば、Google 翻訳は、ブラウザで実行されるクライアント側の JavaScript でその仕事、またはその一部を実行しますが、もちろん Python スクリプトでは実行しません。

Google はTranslate APIを有料サービスとして提供しています。

于 2012-06-22T21:19:39.063 に答える
2

Selenium、http: //seleniumhq.org/ を試すか、PyQT Webkit を使用してください

#! /usr/bin/python

import sys
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView
from PyQt4.QtGui import QApplication

app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://translate.google.com/#en|pt|love"))
web.show()
app.exec_()
print web.page().mainFrame().toPlainText().toUtf8()

アプリウィンドウを閉じると、必要なものが表示されます

于 2012-06-22T21:29:52.120 に答える