綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[教程] Python 網路連線、公開資料串接 下載特定網址資料異常處理

[複製連結] 查看: 24546|回覆: 0|好評: 0
跳轉到指定樓層
樓主
jianrupan | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2022-1-14 16:39

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
# 載入模組
import urllib.request as request
# 指定網址
src = "https://python.org/"
# 開啟網址資料
request.urlopen(src)
執行異常訊息:
Traceback (most recent call last):
  File "...PythonPython39liburllib equest.py", line 1346, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "...PythonPython39libhttpclient.py", line 1253, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "...PythonPython39libhttpclient.py", line 1299, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "...PythonPython39libhttpclient.py", line 1248, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "...PythonPython39libhttpclient.py", line 1008, in _send_output
    self.send(msg)
  File "...PythonPython39libhttpclient.py", line 948, in send
    self.connect()
  File "...PythonPython39libhttpclient.py", line 1422, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "...PythonPython39libssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "...PythonPython39libssl.py", line 1040, in _create
    self.do_handshake()
  File "...PythonPython39libssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "... et_data.py", line 9, in <module>
    request.urlopen(src)
  File "...PythonPython39liburllib equest.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "...PythonPython39liburllib equest.py", line 517, in open
    response = self._open(req, data)
  File "...PythonPython39liburllib equest.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "...PythonPython39liburllib equest.py", line 494, in _call_chain
    result = func(*args)
  File "...PythonPython39liburllib equest.py", line 1389, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "...PythonPython39liburllib equest.py", line 1349, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)>
搜尋異常狀態
# 載入模組
import urllib.request as request
# 指定網址
src = "https://python.org/"
# 開啟網址資料
#request.urlopen(src)
from urllib.error import HTTPError, URLError
try:
    request.urlopen(src)
    print("urlopen OK")
except HTTPError as e:
    print("HTTP code error: ", e.reason)
except URLError as error:
    print("URLError code error: ", error.reason)
顯示執行異常訊息:
URLError code error:  [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)
問題發生原因:
驗證 SSL 憑證異常。
修改說明:選擇不用認證此 SSL 憑證
# 載入模組
import urllib.request as request
import ssl
from urllib.error import HTTPError, URLError
# 指定網址
src = "https://python.org/"
# 取消 SSL 憑證 認證
ssl._create_default_https_context = ssl._create_unverified_context
# 開啟網址資料
try:
    request.urlopen(src)
    print("urlopen OK")
except HTTPError as e:
    print("HTTP code error: ", e.reason)
except URLError as error:
    print("URLError code error: ", error.reason)
顯示正確執行訊息:
urlopen OK

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)

評分

參與人數 3碎鑽 +2 幫助 +3 收起 理由
croy + 1 + 1 非常讃
熊熊寶貝 + 1 + 1 非常讃
傻庭兒 + 1 偶像,看完你的內容,讓我找到了活著的意義.

查看全部評分

收藏收藏2 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則