[英]try and except method to disable error messages
I'm getting AttributeError at /add_post/'NoneType' object has no attribute 'src' whenever I don't provide url which is not a required field. 每当我不提供不是必填字段的url时,我在/ add_post /'NoneType'对象上就出现AttributeError时没有属性'src'。 I just want to use try and catch method so I don't get this error again.
我只想使用try and catch方法,这样就不会再出现此错误。
def extract(url):
g = Goose()
article = g.extract(url=url)
resposne = {'image':article.top_image.src}
return article.top_image.src
the error is occurring from resposne = {'image':article.top_image.src}
the problem of implementing try and except I'm facing is that when I'm getting this error I want some default image to be displayed, but don't know how....if you can help me I would be appreciated. 错误是从
resposne = {'image':article.top_image.src}
的,实现try的问题是我遇到的问题是,当我遇到此错误时,我希望显示一些默认图像,但是不要不知道如何..如果您能帮助我,我将不胜感激。 Thank you 谢谢
Simply check whether top_image is none. 只需检查top_image是否为none。
if article is not None:
if article.top_image is not None:
return {'image':article.top_image.src}
return {'image':'default image'}
If that's the only error then: 如果那是唯一的错误,则:
try:
resposne = {'image':article.top_image.src}
except AttributeError:
resposne = some_image_object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.