📜  检查状态码 urllib - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:47.358000             🧑  作者: Mango

代码示例2
import urllib2

req = urllib2.Request('http://www.python.org/fish.html')
try:
    resp = urllib2.urlopen(req)
except urllib2.HTTPError as e:
    if e.code == 404:
        # do something...
    else:
        # ...
except urllib2.URLError as e:
    # Not an HTTP-specific error (e.g. connection refused)
    # ...
else:
    # 200
    body = resp.read()