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

后端 未结 2 1216

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 05:01

    Running your code in python 2.7.5:

    import requests
    
    try:
      response = requests.get('google.com/admin') #Should return 404
    except requests.HTTPError, e:
      print 'HTTP ERROR %s occured' % e.code
      print e
    

    Results in:

    File "C:\Python27\lib\site-packages\requests\models.py", line 291, in prepare_url raise MissingSchema("Invalid URL %r: No schema supplied" % url) requests.exceptions.MissingSchema: Invalid URL u'google.com/admin': No schema supplied

    To get your code to pick up this exception you need to add:

      except (requests.exceptions.MissingSchema) as e:
        print 'Missing schema occured. status'
        print e
    

    Note also it is not a missing schema but a missing scheme.

提交回复
热议问题