Hello,

This is my first python script, so I could use a little bit of help here. :)

I'm using the 'Universal Feed Parser' (http://feedparser.org/), and I have a list of feed URLs in a database which I would like to pull out and go through each of them. I am also using the ADOdb database module, as I am familier with it (in PHP .. however, it is pretty similar ..)

Here's what I have so far, the only thing this does is pulls the URLs out of the database, and prints them:

#!/usr/bin/env python

import MySQLdb
import adodb
import feedparser

conn = adodb.NewADOConnection('mysql')
conn.Connect('localhost','root','dadada','db');
cursor = conn.Execute('SELECT feed_url FROM feed_urls')

while not cursor.EOF:
    for row in cursor.fields:
        print row
    cursor.MoveNext()

cursor.Close()
conn.Close()

As I said, this above code works perfectly. However, I want to take that output, and put it in something like this:

while not cursor.EOF:
    for row in cursor.fields:
        d = feedparser.parse(FEED_URL_HERE)  <-- This is where I need the output of the db results to appear

So, basically .. the above code is calling a function from the 'Universal Feed Parser' module, which takes in a url .. basically, when it executes, I want python to read it like this:

d = feedparser.parse('http://www.url.com/to/rss.feed')

I know it has got to be something simple. I'd definitely appreciate some help on this. Thank you very much :)

forgive me if I am saying something dumb, but is the output you need being produced by this code

#!/usr/bin/env python

import MySQLdb
import adodb
import feedparser

conn = adodb.NewADOConnection('mysql')
conn.Connect('localhost','root','dadada','db');
cursor = conn.Execute('SELECT feed_url FROM feed_urls')

while not cursor.EOF:
    for row in cursor.fields:
        print row
    cursor.MoveNext()

cursor.Close()
conn.Close()

if so what part of that code is producing the output?

Yes, this snippet actually works. The output is being produced with this line:

print row

'row' is the variable holding the data.

I've tried just doing this:

while not cursor.EOF:
    for row in cursor.fields:
        d = feedparser.parse(row)  <-- This is the variable (row)

..however, that doesn't work.

forgive me if I am saying something dumb, but is the output you need being produced by this code

#!/usr/bin/env python

import MySQLdb
import adodb
import feedparser

conn = adodb.NewADOConnection('mysql')
conn.Connect('localhost','root','dadada','db');
cursor = conn.Execute('SELECT feed_url FROM feed_urls')

while not cursor.EOF:
    for row in cursor.fields:
        print row
    cursor.MoveNext()

cursor.Close()
conn.Close()

if so what part of that code is producing the output?

I am pretty new to python(and programming) but this might work

while not cursor.EOF:
    for row in cursor.fields:
        url = row
        print row
    cursor.MoveNext()

now you should be able to place the varialble url in your function. but the variable url will be changed everytime you loop.

edit adeed later//

Sorry I should have tested that before I posted, I doesn't seem to work the way I thought it would :-(

Hi there,

Thanks for your replies.

The repr function seems to be useful.. Here's my exact problem though:

while not cursor.EOF:
    for row in cursor.fields:
        str1 = repr(row)
        #print str1    #this line works .. if I take out the code below from above cursor.MoveNext()

        d = feedparser.parse(str1)
        d.feed.title

        print d.feed.title
        print "-----------------------------"

    cursor.MoveNext()

If you see the line d = feedparser.parse(str1), I believe this is where the error is occuring. If I simply do print str1, it prints the variable. I just need the variable to be accessable within the feedparser.parse function.

Here's the error I get with the above code:

[root@localhost feedparser]# ./parse.py
Traceback (most recent call last):
  File "./parse.py", line 17, in ?
    d.feed.title
  File "/home/matt/feedparser/feedparser.py", line 184, in __getattr__
    raise AttributeError, "object has no attribute '%s'" % key
AttributeError: object has no attribute 'title'

I believe that it is simply not able to access the url contained in the variable (hence the no 'title' attribute)..

Any ideas? This has been bugging me for quite a while... :) Again, thank you for your time!

Ah, you know what .. :) It turns out that the code that I thought was wrong, was actually right (boy did I think Python was extremely complex there for a few moments :)). I was simply not reading the values returned from Universal Feed Parser correctly.

Thank you for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.