Python不连接字符串和unicode来链接 [英] Python not concatenating string and unicode to link

查看:27
本文介绍了Python不连接字符串和unicode来链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 str 的末尾附加一个 Unicode 字符串时,我无法点击 URL.

When I append a Unicode string to the end of str, I can not click on the URL.

不好:

base_url = 'https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles='

url = base_url + u"Ángel_Garasa"
print url

好:

base_url = 'https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles='

url = base_url + u"Toby_Maquire"
print url

推荐答案

您似乎是在 IDE(可能是 PyCharm)中打印结果.您需要对字符串的 UTF-8 编码版本进行百分比编码:

It appears that you're printing the results in an IDE, perhaps PyCharm. You need to percent encode a UTF-8 encoded version of the string:

import urllib

base_url = 'https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles='
name = u"Ángel_Garasa"

print base_url + urllib.quote(name.encode("utf-8"))

这显示:

在您的情况下,您需要更新代码,以便数据库中的相关字段进行百分比编码.您只需要将此字段编码为 UTF-8 即可用于百分比编码.

In your case you need to update your code, so that the relevant field from the database is percent encoded. You only need to encode this one field to UTF-8 just for the percent encoding.

这篇关于Python不连接字符串和unicode来链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆