Re: [Tutor] using a for loop with feedparser

2009-01-26 Thread Tonu Mikk

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@python.org
http://mail.python.org/mailman/listinfo/tutor



--
Tonu Mikk
Educational Technology Consultant
Digital Media Center - dmc.umn.edu
tm...@umn.edu 612 625-9221

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using a for loop with feedparser

2009-01-26 Thread Tonu Mikk
I now have a new question related to the same project.  I currently have 
a code snippet like this:


1.for item in d.entries:
2.link = Link(url=item.link)
3.link.save()
4.user = User.objects.get(id=1)
5.link = Link.objects.get(id=1)
6.bookmark = Bookmark (
7.title = item.title,
8.desc = item.description,
9.link = link,
10.  user = user,
11.  )
12.  bookmark.save()

I need to increment the link id (line 5) for each item in the 
d.entries.  I tried link = Link.objects.get(id=id+1) and link = 
Link.objects.get((id=1)+1), but both of them generated errors.   I am 
not quite sure where to go from here.


Thank you,
Tonu

Tonu Mikk wrote:

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@python.org
http://mail.python.org/mailman/listinfo/tutor






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using a for loop with feedparser

2009-01-26 Thread Kent Johnson
On Mon, Jan 26, 2009 at 4:53 PM, Tonu Mikk tm...@umn.edu wrote:
 I now have a new question related to the same project.  I currently have a
 code snippet like this:

 1.for item in d.entries:
 2.link = Link(url=item.link)
 3.link.save()
 4.user = User.objects.get(id=1)
 5.link = Link.objects.get(id=1)
 6.bookmark = Bookmark (
 7.title = item.title,
 8.desc = item.description,
 9.link = link,
 10.  user = user,
 11.  )
 12.  bookmark.save()

 I need to increment the link id (line 5) for each item in the d.entries.  I
 tried link = Link.objects.get(id=id+1) and link =
 Link.objects.get((id=1)+1), but both of them generated errors.   I am not
 quite sure where to go from here.

Are you trying to retrieve the Link you just saved in line 3? Why not
just use the link variable you already have?

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor