简体   繁体   English

urllib请求中的python 3.6 ascii编解码器错误

[英]python 3.6 ascii codec error in urllib request

I'm trying to download picture from website with my python script, but every time i use georgian alphabet in url it gets error "UnicodeEncodeError: 'ascii' codec can't encode characters" 我正在尝试使用python脚本从网站下载图片,但是每次我在URL中使用格鲁吉亚字母时,都会收到错误消息“ UnicodeEncodeError:'ascii'编解码器无法编码字符”

here is my code: 这是我的代码:

import os
import urllib.request

def download_image(url):
    fullfilename = os.path.join('/images', 'image.jpg')

    urllib.request.urlretrieve(url, fullfilename)


download_image(u'https://example.com/media/სდასდსადადსაფა_8QXjrbi.jpg')

I think it's better to use requests library in your example which deals with utf-8 characters. 我认为在示例中使用处理utf-8字符的请求库更好。

Here is the code: 这是代码:

import requests

def download_image(url):

    request = requests.get(url)
    local_path = 'images/images.jpg'

    with open(local_path, 'wb') as file:  
        file.write(request.content)

my_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/ერეკლე_II_ბავშვობის_სურათი.jpgw/459px-ერეკლე_II_ბავშვობის_სურათი.jpg'

download_image(my_url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM