try和except方法来禁用错误消息 [英] try and except method to disable error messages
问题描述
我在/ add_post /'NoneType'中得到AttributeError当我不提供不是必填字段的url时,对象没有属性'src'
。我只想使用try和catch方法,所以我不会再收到这个错误。
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. I just want to use try and catch method so I don't get this error again.
def extract(url):
g = Goose()
article = g.extract(url=url)
resposne = {'image':article.top_image.src}
return article.top_image.src
错误发生在 resposne = {'image':article.top_image.src
实现try的问题,除了我面临的是,当我收到这个错误,我想要一些默认图像显示,但不知道如何....如果你能帮助我,我将不胜感激。谢谢
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. Thank you
推荐答案
只需检查top_image是否为none。
Simply check whether top_image is none.
if article is not None:
if article.top_image is not None:
return {'image':article.top_image.src}
return {'image':'default image'}
这篇关于try和except方法来禁用错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!