0

Im trying to download a picture from the internet but once I use string formatting it gives me an Error: "UnicodeEncodeError: 'ascii' codec can't encode character '\xdf' in position 161: ordinal not in range(128)" If I remove string formatting the picture is downloadable.

I have already tried to encode and decode but nothings works.

def get_location(self):
    self.key = self.key_entry.get()
    self.location = self.location_entry.get()
    self.zoom = self.zoom_entry.get()
    self.type = self.type_entry.get()
    self.url = "https://www.mapquestapi.com/staticmap/v4/getplacemap?key=%s&size=600,600&type=%s&imagetype=png&zoom=%s&scalebar=false&traffic=false&location=%s" % (self.key, self.type, self.zoom, self.location)
    webbrowser.open_new_tab('%s' % self.url)
    urllib.request.urlretrieve(self.url, "location.png")

1 Answers1

0

try using utf-8

webbrowser.open_new_tab('%s' % self.url, 'utf-8')
RomanHDev
  • 104
  • 5
  • Oh i forgot to mention that opening the Picture is not the problem but downloading it causes it : "urllib.request.urlretrieve(self.url, "location.png") Does this work on the urrllib.. also? – Marcel Pohl Apr 16 '19 at 10:07
  • you could try setting global encoding to utf-8. https://stackoverflow.com/questions/2276200/changing-default-encoding-of-python – RomanHDev Apr 16 '19 at 10:55
  • urllib seems to have a distinct parser for your purpose. Take a look at this: https://stackoverflow.com/a/18269491/11367159 – RomanHDev Apr 16 '19 at 10:59
  • Thanks for your help i already found another solution i just used the requests library and downloaded it there – Marcel Pohl Apr 16 '19 at 11:43