美文网首页
urllib.error

urllib.error

作者: 是我真的是我 | 来源:发表于2019-10-17 16:41 被阅读0次
import urllib.request
import urllib.error

'地址异常'
try:
    req = urllib.request.Request('http://www.black_html.com')
    urllib.request.urlopen(req)
    print('ok')
except urllib.error.URLError as e:
    print(e.reason)

# [Errno 11001] getaddrinfo failed



'编码异常:此例需要保证地址正确'
try:
    req = urllib.request.Request('http://www.jianshu.com/index.html')
    urllib.request.urlopen(req)
    print('ok')
except urllib.error.HTTPError as e:
    print(e.code)
    print(e.read().decode())

# 403
# <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
# <html>
# <head><title>403 Forbidden</title></head>
# <body bgcolor="white">
# <h1>403 Forbidden</h1>
# <p>You don't have permission to access the URL on this server. Sorry for the inconvenience.<br/>
# Please report this message and include the following information to us.<br/>
# Thank you very much!</p>
# <table>
# <tr>
# <td>URL:</td>
# <td>https://www.jianshu.com/index.html</td>
# </tr>
# <tr>
# <td>Server:</td>
# <td>zurich</td>
# </tr>
# <tr>
# <td>Date:</td>
# <td>2019/10/17 16:05:35</td>
# </tr>
# </table>
# <hr/>Powered by Tengine</body>
# </html>

相关文章

网友评论

      本文标题:urllib.error

      本文链接:https://www.haomeiwen.com/subject/cylvmctx.html