[Tutor] using a for loop with feedparser
Tonu Mikk
tmikk at umn.edu
Mon Jan 26 22:22:41 CET 2009
Tonu Mikk wrote:
> 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?
I found out the answer after trying some more. To print the title only,
I need to do this:
for item in d.entries:
print item.title
>
>
> 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()
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Tonu Mikk
Educational Technology Consultant
Digital Media Center - dmc.umn.edu
tmikk at umn.edu 612 625-9221
More information about the Tutor
mailing list