它发生在除了ParseError之外的那一行,我不明白,因为我写这一行是为了避免这个错误。
import json
from goose import Goose
DEFAULT = 'https://images.unsplash.com/photo-1427435150519-42d9bcd0aa81?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=10d1cfc208c8ed5ed1160e851eabce1d'
def extract(url):
g = Goose()
try:
article = g.extract(url=url)
if article.top_image is None:
return DEFAULT
else:
if article.top_image.src is None:
return DEFAULT
else:
resposne = {'image':article.top_image.src}
return article.top_image.src
except ParseError:
if can_handle():
handle_exception()
else:
print("couldn't handle exception: url={0}".format(url))
raise
我主要只是想避免这个错误,如果我得到这个错误,我只想显示默认图像。我错过了什么?
这是我的回溯
Traceback:
File "/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
34. return bound_func(*args, **kwargs)
File "/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
30. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/younggue/Desktop/ebagu0.2/rclone/main/views.py" in dispatch
172. return super(PostCreateView, self).dispatch(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
249. return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
215. return self.form_valid(form)
File "main/views.py" in form_valid
165. self.object.image = extract(self.object.url)
File "main/util/media.py" in extract
18. except ParseError:
Exception Type: NameError at /add_post/
Exception Value: global name 'ParseError' is not defined
2条答案
按热度按时间ggazkfy81#
我在使用panda从文件列表中迭代加载文件时也遇到了同样的错误,在运行代码后,我在堆栈跟踪中发现了
lib/site-packages/pandas/io/parsers.py
。为了纠正这个问题,我必须从定义错误的文件中导入该错误。
hzbexzde2#
答案是here
或简单