In Python, how do I use urllib to see if a website is 404 or 200?

后端 未结 4 399
北荒
北荒 2020-12-02 07:45

How to get the code of the headers through urllib?

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 08:31

    You can use urllib2 as well:

    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()
    

    Note that HTTPError is a subclass of URLError which stores the HTTP status code.

提交回复
热议问题