Hi I am trying to save images through URL on python 2.7. There are some urls like containing special characters.
like : http://homegrown.co.in/wp-content/uploads/2014/06/Pl%C3%B6tzlich-Am-Meer.jpg
and there are lots of url with some special character.
I am saving URL by following code and I am getting error:
def save_image_from_url(url, filename):
print('Saving {} locally'.format(url))
image = requests.get(url)
with open(os.path.join(IMG_DIR_ABS, filename), 'wb') as f:
f.write(image.content)
f.close()
Error
File "/home/wp-migrate/migrate.py", line 340, in seperate_img_blocks
save_image_from_url(url, filename)
File "/home/wp-migrate/s3.py", line 50, in save_image_from_url
with open(os.path.join(IMG_DIR_ABS, filename), 'wb') as f:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 16: ordinal not in range(128)
How should I catch this error and continue my process. As I am running continued loop which gets stopped at this point. If there is a solution to save images from these url then its great otherwise please help me to bypass this error and continue my process. Inside my for loop I am importing this function from other file.
No after doing sys.reload
, I am still facing the same problem.