urlopen错误 [Errno 11001] getaddrinfo失败

11 投票
2 回答
88276 浏览
提问于 2025-04-17 20:38

我刚开始学习Python,正在看一个视频教程。

这是代码片段:

from urllib.request import urlopen
with urlopen('http://sixty-north/c/t.txt') as story:
    story_words = []
    for line in story:
        line_words = line.decode('utf-8').split()
        for word in line_words:
            story_words.append(word)

我可以在浏览器里打开 http://sixty-north.com/c/t.txt 这个链接。

但是当我在命令提示符里输入 python words.py 时,出现了这个错误:

C:\New folder>python words.py
Traceback (most recent call last):
  File "C:\Python33\lib\urllib\request.py", line 1248, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python33\lib\http\client.py", line 1065, in request
    self._send_request(method, url, body, headers)
  File "C:\Python33\lib\http\client.py", line 1103, in _send_request
    self.endheaders(body)
  File "C:\Python33\lib\http\client.py", line 1061, in endheaders
    self._send_output(message_body)
  File "C:\Python33\lib\http\client.py", line 906, in _send_output
    self.send(msg)
  File "C:\Python33\lib\http\client.py", line 844, in send
    self.connect()
  File "C:\Python33\lib\http\client.py", line 822, in connect
    self.timeout, self.source_address)
  File "C:\Python33\lib\socket.py", line 417, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "words.py", line 2, in <module>
    with urlopen('http://sixty-north/c/t.txt') as story:
  File "C:\Python33\lib\urllib\request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python33\lib\urllib\request.py", line 469, in open
    response = self._open(req, data)
  File "C:\Python33\lib\urllib\request.py", line 487, in _open
    '_open', req)
  File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
    result = func(*args)
  File "C:\Python33\lib\urllib\request.py", line 1274, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python33\lib\urllib\request.py", line 1251, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>

2 个回答

0

把主机名 sixty-north 改成 sixty-north.com
另外,重启一下你的网络连接。
你的网络连接有点问题。

9

找不到这个主机:sixty-north。请把sixty-north换成sixty-north.com(注意:最后要加上.com

撰写回答