requests.HTTPError uncaught after a requests.get() 404 response

后端 未结 2 1207

I\'m having a slight problem with the requests library.

Say for example I have a statement like this in Python:

try:
   request = requests.get(\'goog         


        
2条回答
  •  独厮守ぢ
    2021-01-07 04:56

    Interpreter is your friend:

    import requests
    requests.get('google.com/admin')
    # MissingSchema: Invalid URL u'google.com/admin': No schema supplied
    

    Also, requests exceptions:

    import requests.exceptions
    dir(requests.exceptions)
    

    Also notice that by default requests doesn't raise exception if status is not 200:

    In [9]: requests.get('https://google.com/admin')
    Out[9]: 
    

    There is raise_for_status() method that does it:

    In [10]: resp = requests.get('https://google.com/admin')
    
    In [11]: resp
    Out[11]: 
    
    In [12]: resp.raise_for_status()
      ...
    HTTPError: 503 Server Error: Service Unavailable
    

提交回复
热议问题