Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用urllib在Python2中读取带有Unicode字符的图像url?_Python_Python 2.7_Url_Urllib2_Anki - Fatal编程技术网

如何使用urllib在Python2中读取带有Unicode字符的图像url?

如何使用urllib在Python2中读取带有Unicode字符的图像url?,python,python-2.7,url,urllib2,anki,Python,Python 2.7,Url,Urllib2,Anki,要求:Python 2.7,没有像Requests或BeautifulSoup这样的外部库:( 调用此url时,我从retrieveUrl中得到回溯错误: u'http://%E7%9F%A5%E3%81%A3%E5%BE%97%E8%A2%8B.biz/wp content/uploads/2016/10/104743-300x225.jpg' 正如您所看到的,我的服务器已经为我提供了一个很好的url,并且已经准备好了,但是它仍然失败了 def retrieveUrl(url): re

要求:Python 2.7,没有像Requests或BeautifulSoup这样的外部库:(

调用此url时,我从retrieveUrl中得到回溯错误:
u'http://%E7%9F%A5%E3%81%A3%E5%BE%97%E8%A2%8B.biz/wp content/uploads/2016/10/104743-300x225.jpg'

正如您所看到的,我的服务器已经为我提供了一个很好的url,并且已经准备好了,但是它仍然失败了

def retrieveUrl(url):
    req = urllib2.Request(url, None, {'User-Agent': 'Mozilla/5.0 (compatible; Anki)'})
    filecontents = urllib2.urlopen(req).read()
    path = unicode(urllib2.unquote(url.encode("utf8")), "utf8")
    filename, file_extension = os.path.splitext(path)
    return filename, file_extension, filecontents
错误回溯

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 431, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 449, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1227, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1194, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "C:\Python27\lib\httplib.py", line 1057, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 1097, in _send_request
    self.endheaders(body)
  File "C:\Python27\lib\httplib.py", line 1053, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 897, in _send_output
    self.send(msg)
  File "C:\Python27\lib\httplib.py", line 859, in send
    self.connect()
  File "C:\Python27\lib\httplib.py", line 836, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Python27\lib\encodings\idna.py", line 164, in encode
    result.append(ToASCII(label))
  File "C:\Python27\lib\encodings\idna.py", line 76, in ToASCII
    label = nameprep(label)
  File "C:\Python27\lib\encodings\idna.py", line 38, in nameprep
    raise UnicodeError("Invalid character %r" % c)
UnicodeError: Invalid character u'\x9f'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\urllib2.py”,第154行,在urlopen中
返回opener.open(url、数据、超时)
文件“C:\Python27\lib\urllib2.py”,第431行,打开
响应=自身打开(请求,数据)
文件“C:\Python27\lib\urllib2.py”,第449行,处于打开状态
"开放",
文件“C:\Python27\lib\urllib2.py”,第409行,在调用链中
结果=func(*args)
文件“C:\Python27\lib\urllib2.py”,第1227行,在http\u open中
返回self.do_open(httplib.HTTPConnection,req)
文件“C:\Python27\lib\urllib2.py”,第1194行,打开
h、 请求(请求获取方法(),请求获取选择器(),请求数据,标题)
请求中第1057行的文件“C:\Python27\lib\httplib.py”
self.\u发送请求(方法、url、正文、标题)
文件“C:\Python27\lib\httplib.py”,第1097行,在发送请求中
self.endheaders(主体)
文件“C:\Python27\lib\httplib.py”,第1053行,在endheaders中
自发送输出(消息体)
文件“C:\Python27\lib\httplib.py”,第897行,在发送输出中
self.send(msg)
文件“C:\Python27\lib\httplib.py”,第859行,在send中
self.connect()
文件“C:\Python27\lib\httplib.py”,第836行,在connect中
self.timeout,self.source\u地址)
文件“C:\Python27\lib\socket.py”,第557行,在create\u connection中
对于getaddrinfo(主机、端口、0、SOCK_流)中的res:
文件“C:\Python27\lib\encodings\idna.py”,第164行,在encode中
结果.附加(ToASCII(标签))
文件“C:\Python27\lib\encodings\idna.py”,第76行,在ToASCII中
标签=名称准备(标签)
文件“C:\Python27\lib\encodings\idna.py”,第38行,在nameprep中
引发Unicode错误(“无效字符%r”%c)
UnicodeError:无效字符u'\x9f'
我甚至还没有弄清楚u'\x9f'是什么字符。 有没有办法修复该函数以获取文件内容