gpt4 book ai didi

python - 特定于语言的重定向

转载 作者:太空宇宙 更新时间:2023-11-03 19:35:00 25 4
gpt4 key购买 nike

我想创建一个简单的应用程序来检测短语的语言(使用 Google API)并将其发送到相应的搜索引擎。例如,如果搜索查询是俄语的,那么我需要将其重定向到 Yandex.ru,而在所有其他情况下,我需要将其重定向到 Google。

我就是这样做的:

def get(self):                                                                        
decoded = unicode(unquote(self.request.query), "windows-1251")
text = decoded.encode("utf-8")
url = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q="+ quote(text)

try:
data = json.loads(urllib2.urlopen(url).read())
redirectUrl = "http://www.google.com/search?q=" + text
if data["responseData"]["language"] == 'ru':
redirectUrl = "http://yandex.ru/yandsearch?text=" + text
self.redirect(redirectUrl)
except urllib2.HTTPError, e:
self.response.out.write( "HTTP error: %d" % e.code )
except urllib2.URLError, e:
self.response.out.write( "Network error: %s" % e.reason.args[1])

当我请求此网址“http://findinrightplace.appspot.com/q?test query”时,它会重定向到 google,但重定向到 yandex 不起作用 (http://findinrightplace.appspot.com/q?тестовый запрос)。

我做错了什么?

最佳答案

您需要从 url = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q="+ quote(text) 中删除 quote() ,它为您的俄语查询返回了错误的结果。

我在本地 python shell 中测试了您的代码,它在没有 quote() 的情况下工作,但在使用 quote() 时不起作用。

关于python - 特定于语言的重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4022332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com