RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 3384 | Replies: 6
Reply
Join Date: Jul 2005
Posts: 5
Reputation: mattd2 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mattd2 mattd2 is offline Offline
Newbie Poster

Moving to Python From PHP: Using a variable in a function

  #1  
Jul 23rd, 2005
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Moving to Python From PHP: Using a variable in a function

  #2  
Jul 24th, 2005
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?
Reply With Quote  
Join Date: Jul 2005
Posts: 5
Reputation: mattd2 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mattd2 mattd2 is offline Offline
Newbie Poster

Re: Moving to Python From PHP: Using a variable in a function

  #3  
Jul 24th, 2005
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.


Originally Posted by shanenin
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?
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Moving to Python From PHP: Using a variable in a function

  #4  
Jul 24th, 2005
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 :-(
Reply With Quote  
Join Date: Oct 2004
Posts: 2,548
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: Moving to Python From PHP: Using a variable in a function

  #5  
Jul 24th, 2005
Sounds like the function repr() might just do the trick ...
[php]str1 = 'http://www.url.com/to/rss.feed'

print str1 # result = http://www.url.com/to/rss.feed

print repr(str1) # result = 'http://www.url.com/to/rss.feed'
[/php]
May 'the Google' be with you!
Reply With Quote  
Join Date: Jul 2005
Posts: 5
Reputation: mattd2 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mattd2 mattd2 is offline Offline
Newbie Poster

Re: Moving to Python From PHP: Using a variable in a function

  #6  
Jul 24th, 2005
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!
Reply With Quote  
Join Date: Jul 2005
Posts: 5
Reputation: mattd2 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mattd2 mattd2 is offline Offline
Newbie Poster

Re: Moving to Python From PHP: Using a variable in a function

  #7  
Jul 24th, 2005
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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:58 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC