tree em
tree em

Reputation: 21711

problem of urlretrieve cannot get image from url contains unicode string

I write a python script to retrieve the image from url:

url = `https://uploads0.wikiart.org/images/albrecht-durer/watermill-at-the-montaсa.jpg`
urllib.request.urlretrieve(url, STYLE_IMAGE_UPLOAD + "wikiart" + "/" + url)

When I run I got the message

UnicodeEncodeError: 'ascii' codec can't encode character '\u0441' in position 49: ordinal not in range(128)

I think the problem from the image url

'https://uploads0.wikiart.org/images/albrecht-durer/watermill-at-the-monta\u0441a.jpg',

How to fix this problem?

Upvotes: 0

Views: 405

Answers (1)

lenz
lenz

Reputation: 5817

The URL contains a non-ASCII character (a Cyrillic letter that looks like a Latin "c").

Escape this character using the urllib.parse.quote function:

url = 'https://uploads0.wikiart.org' + urllib.parse.quote('/images/albrecht-durer/watermill-at-the-montaсa.jpg')
urllib.request.urlretrieve(url, '/tmp/watermill.jpg')

Don't put the entire URL in the quote function, otherwise it would escape the colon (":") in "https://".

Upvotes: 1

Related Questions