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

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

How to get the code of the headers through urllib?

4条回答
  •  被撕碎了的回忆
    2020-12-02 08:37

    The getcode() method (Added in python2.6) returns the HTTP status code that was sent with the response, or None if the URL is no HTTP URL.

    >>> a=urllib.urlopen('http://www.google.com/asdfsf')
    >>> a.getcode()
    404
    >>> a=urllib.urlopen('http://www.google.com/')
    >>> a.getcode()
    200
    

提交回复
热议问题