python - urllib.error.URLError : <urlopen error timed out>

标签 python python-3.x

我是 python 新手,正在使用 python 3.5.0。我试图实现一个简单的代码如下:

import urllib.request
page = urllib.request.urlopen("http://www.google.com",timeout=20)
text = page.read().decode("utf8")
print(text)

但没想到我收到了以下错误:

Traceback (most recent call last):
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1240, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1083, in request
    self._send_request(method, url, body, headers)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1128, in _send_request
    self.endheaders(body)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1079, in endheaders
    self._send_output(message_body)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 911, in _send_output
    self.send(msg)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 854, in send
    self.connect()
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 826, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 707, in create_connection
    raise err
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 698, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Python\Test_Run.py", line 2, in <module>
    page = urllib.request.urlopen("http://www.google.com",timeout=20)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 465, in open
    response = self._open(req, data)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 483, in _open
    '_open', req)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
    result = func(*args)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1268, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1242, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error timed out>

我已连接到我的大学网络。这就是我得到这个的原因吗?我该怎么办?

最佳答案

另一个答案没有专门处理超时异常情况。处理超时情况:

try:
    response = urllib.request.urlopen(request, timeout=10)
    return response
except urllib.error.URLError as e:
    if isinstance(e.reason, socket.timeout):
        # handle timeout...
        pass
    raise e

关于 python - urllib.error.URLError : <urlopen error timed out>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38662135/

相关文章:

python - 使用 Selenium 在 Python 中循环 XPath 元素位置

python - 等效于 csv 文件的 Keras ImageDataGenerator

python - 使用keras进行多类分类

python - 你如何改变pandas box plot的Y轴的传播?

python - 使用seaborn绘制pandas数据框系列

python - Django 休息框架 : Filtering against Table Field value

python-3.x - django allauth 中的 Django 排序字段

css - 如何在 python3 中替换非标准标签的属性?

python - 为什么 .isascii() 在 google colab 上不起作用?

python - Jupyter Notebook 图形大小设置