我想删除重复的x.raise_for_status()行:
x = requests.get(url1) x.raise_for_status() y = requests.delete(url2) y.raise_for_status() z = requests.post(url3,data={'foo': 'bar'}) z.raise_for_status()
解决方法
使用钩子创建会话:
session = requests.Session() session.hooks = { 'response': lambda r,*args,**kwargs: r.raise_for_status() } x = session.get(url1) y = session.delete(url2) z = session.post(url3,data={'foo': 'bar'})