Python does not concatenate string and unicode for reference

When I add a Unicode line to the end of the line, I cannot click on the URL.

Poorly:

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

The end result for a name with accents

Good:

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

The end result for a name without accents

0
source share
1 answer

It looks like you're printing the results in an IDE, perhaps PyCharm. You need the percentage to encode the encoded version of the string in UTF-8 format:

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"))

It shows: image showing clickable link with percentage encoded part

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

+1
source

Source: https://habr.com/ru/post/1622463/


All Articles