[Tutor] using a for loop with feedparser

Tonu Mikk tmikk at umn.edu
Mon Jan 26 21:55:00 CET 2009


Hello,

I am trying to parse a feed and insert some feed elements into a Django 
project database.   I believe I should be able to use for loop to 
iterate over the list elements.  I am using the Universal Feed parser 
(http://www.feedparser.org/).  I am able to extract items one by one and 
insert them into a database (code below).   As my first try at this, I 
would like to print the titles of all the feed entries.  I tried this

import feedparser
d = feedparser.parse('http://feeds.delicious.com/v2/rss/utools')
for item in d.entries:
    print item

This prints all the feed entries and their values.  Any thoughts on how 
I could just print the titles? 

Thank you,
Tonu


Code to save a single feed item into the database:
#!/usr/local/bin/python
import feedparser
import os.path
import sys, os
import django.contrib.auth
sys.path.append ('/home/dmc/projects/django_bookmarks')
os.environ['DJANGO_SETTINGS_MODULE']='settings'
from django.contrib.auth.models import User
from bookmarks.models import *
d = feedparser.parse('http://feeds.delicious.com/v2/rss/utools')

link1 = Link(url=d.entries[0].link)
link1.save()
user = User.objects.get(id=1)
link = Link.objects.get(id=1)
bookmark = Bookmark (
    title = d.entries[0].title,
    desc = d.entries[0].description,
    link = link,
    user = user,
)
bookmark.save()


More information about the Tutor mailing list