Django rss feedparser returns a feed with no “title”

耗尽温柔 提交于 2019-12-22 06:35:25

问题


I'm writing a basic RSS feed reader in Django. I have a form in which a user submits a rss feed, and I add it to his feeds list. But for some reason, I'm unable to extract basic information about the feed using feed parser.

when i run the following code:

def form_valid(self, form):
    user = self.request.user
    link = form.cleaned_data['link']
    feed = feedparser.parse(link).feed
    title = feed.title
    try:
        feed_obj = Feed.objects.get(link=link)
    except ObjectDoesNotExist:
        feed_obj = Feed(link=link, title=title)
        feed_obj.save()
    user.get_profile().feeds.add(feed_obj)
    return super(DashboardView, self).form_valid(form)

Django throws me an "object has no attribute 'title'" exception on line 5:

title = feed.title

Full error details are:

Traceback:
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
  165.             return self.form_valid(form)
File "/home/yaniv/nextfeed/profiles/views.py" in form_valid
  48.         title = feed.title
File "/home/yaniv/nextfeed/venv/local/lib/python2.7/site-packages/feedparser.py" in __getattr__
  416.             raise AttributeError, "object has no attribute '%s'" % key

Exception Type: AttributeError at /dashboard
Exception Value: object has no attribute 'title'

What am I doing wrong?

EDIT: I traced the program with pdb. Right before the problematic line, I got:

(Pdb) link
u'http://feedparser.org/docs/examples/rss20.xml'
(Pdb) feed
{'xhtml_script': {'type': u'text/javascript', 'language': u'javascript'}, 'summary': u''}

回答1:


It's been a while since I used feedparser, but IIRC, the parser returns a dictionary, like so:

foo = feedparser.parse(link)
feed = foo['feed']
title = feed['title']

You seem to have gotten an object back from foo.feed, but that's not what you want.



来源:https://stackoverflow.com/questions/18231057/django-rss-feedparser-returns-a-feed-with-no-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!