URLError:<urlopen error =“” [errno =“” 11001] =“” getaddrinfo =“” failed =“”>,为什么?

时间:2018-09-19 09:52:34

标签: python

    import http.cookiejar
    import urllib.request


    cookie=http.cookiejar.MozillaCookieJar()
    cookie.load('cookie.txt',ignore_discard=True,ignore_expires=True)
    req=urllib.request.Request("http://www.baidu,.com")
    opener=urllib.request.build_opener(
           urllib.request.HTTPCookieProcessor(cookie))
    response=opener.open(req)
    print(response.read())

##错误按摩##     错误消息:     gaierror Traceback(最近一次通话)     D:\ Anaconda \ lib \ urllib \ request.py在do_open中(self,http_class,req,** http_conn_args)        1317 h.request(req.get_method(),req.selector,req.data,标头,     -> 1318 encode_chunked = req.has_header('Transfer-encoding'))        1319除了OSError为err:#超时错误

D:\Anaconda\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
   1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
   1240 

D:\Anaconda\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1284             body = _encode(body, 'body')
-> 1285         self.endheaders(body, encode_chunked=encode_chunked)
   1286 

D:\Anaconda\lib\http\client.py in endheaders(self, message_body, encode_chunked)
   1233             raise CannotSendHeader()
-> 1234         self._send_output(message_body, encode_chunked=encode_chunked)
   1235 

D:\Anaconda\lib\http\client.py in _send_output(self, message_body, encode_chunked)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 

D:\Anaconda\lib\http\client.py in send(self, data)
    963             if self.auto_open:
--> 964                 self.connect()
    965             else:

D:\Anaconda\lib\http\client.py in connect(self)
    935         self.sock = self._create_connection(
--> 936             (self.host,self.port), self.timeout, self.source_address)
    937         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

D:\Anaconda\lib\socket.py in create_connection(address, timeout, source_address)
    703     err = None
--> 704     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    705         af, socktype, proto, canonname, sa = res

D:\Anaconda\lib\socket.py in getaddrinfo(host, port, family, type, proto, flags)
    744     addrlist = []
--> 745     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    746         af, socktype, proto, canonname, sa = res

gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
<ipython-input-64-973c2d9c2253> in <module>()
     10 #利用urllib的build_opener方法来创建一个opener
     11 opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
---> 12 response=opener.open(req)
     13 print(response.read())

D:\Anaconda\lib\urllib\request.py in open(self, fullurl, data, timeout)
    524             req = meth(req)
    525 
--> 526         response = self._open(req, data)
    527 
    528         # post-process response

D:\Anaconda\lib\urllib\request.py in _open(self, req, data)
    542         protocol = req.type
    543         result = self._call_chain(self.handle_open, protocol, protocol +
--> 544                                   '_open', req)
    545         if result:
    546             return result

D:\Anaconda\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    502         for handler in handlers:
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:
    506                 return result

D:\Anaconda\lib\urllib\request.py in http_open(self, req)
   1344 
   1345     def http_open(self, req):
-> 1346         return self.do_open(http.client.HTTPConnection, req)
   1347 
   1348     http_request = AbstractHTTPHandler.do_request_

D:\Anaconda\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
   1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error
-> 1320                 raise URLError(err)
   1321             r = h.getresponse()
   1322         except:

URLError: <urlopen error [Errno 11001] getaddrinfo failed>

0 个答案:

没有答案