使用 google 翻译 python 脚本翻译 url
我正在尝试从 python 脚本使用谷歌翻译:
#!/usr/bin/env python
from urllib2 import urlopen
from urllib import urlencode
base_url = "http://www.google.com/translate?"
params = (('langpair','en|es'), ('u','http://asdf.com'),)
url = base_url+urlencode(params)
print "Encoded URL: %s" % url
print urlopen(url).read()
当我使用它时,我收到错误 403。
# ./1.py
Encoded URL: http://www.google.com/translate?langpair=en%7Ces&u=http%3A%2F%2Fasdf.com
Traceback (most recent call last):
...
urllib2.HTTPError: HTTP Error 403: Forbidden
但是,从浏览器访问时,相同的 URL 可以正常工作。有人能发现错误吗?或者谷歌不允许这种类型的使用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果 Google 不允许您这样做,您可以通过 Google 的 API 以编程方式翻译普通网站的源代码。
我不久前为此编写了一个函数:
If Google doesn't let you do this, you could programatically translate the normal website's source via the Google's APIs.
I wrote a function for this a little while back:
您应该使用 google API。我找到并测试了这段代码,它有效:
来源
You should be using the google API. I found and tested this code, it works:
source
您想要使用官方 Google Translate API:
http:// /code.google.com/intl/de-DE/apis/language/translate/overview.html
除此之外:
http://www.catonmat.net/blog/python-library-for-google-search/
You want to use the offical Google Translate APIs:
http://code.google.com/intl/de-DE/apis/language/translate/overview.html
Apart from that:
http://www.catonmat.net/blog/python-library-for-google-search/
你的问题是因为你没有标题
(它告诉谷歌你的浏览器和兼容性是什么)
我之前在制作谷歌翻译API时遇到过这个错误,
你可以在这里找到它: https://github.com/mouuff/Google-Translate-API
your problem is because you have no headers
(which tells google what is your browser and compatibility)
I had this error before when I made my google translate api
you can find it here: https://github.com/mouuff/Google-Translate-API
你可以使用更好的Python代码来使用谷歌进行翻译:
来源:https://neculaifantanaru.com/en/python-code-text-google-translate-website-translation-beautifulsoup-new.html
you can use a much better python code for translating with google:
SOURCE: https://neculaifantanaru.com/en/python-code-text-google-translate-website-translation-beautifulsoup-new.html
importocks
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 10808) socket.socket =ocks.socksocket #使用socks建立连接
import socks
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 10808) socket.socket = socks.socksocket #使用socks建立连接