中文 URL 还是比英文+拼音 URL 比较友好,对 SEO 来说。但在BAE 上容易出现乱码,这是在python 的环境,其它环境可能不一样。
例如网址 http://bae4py.duapp.com/tag/中文
,在浏览器里输入该网址是可以直接打开,打开之后复制地址栏的网址就变成http://bae4py.duapp.com/tag/%E4%B8%AD%E6%96%87
。
这里会用到urllib 库的 unquote
, quote
函数,urllib2
里也有这两个相同的函数。
1
2
3
4
5
6
7
8
9
10
11
# -*- coding: utf-8 -*-
from urllib import unquote, quote
s = u"中文"
s2 = quote(s.encode('utf-8')) #%E4%B8%AD%E6%96%87
s3 = unquote(s2).decode('utf-8') #中文
print s
print s2
print s3
会输出:
1
2
3
中文
%E4%B8%AD%E6%96%87
中文
在BAE 有点不一样,以tornado 框架的一个get 请求为例:
1
2
3
class TagPage(tornado.web.RequestHandler):
def get(self, tag):
tag = unquote(tag.encode('utf-8')).decode('utf-8')
关键是第3行,第一步要对tag encode 一下。