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

Thread Solved

Join Date: Jul 2005
Posts: 5
Reputation: mattd2 is an unknown quantity at this point 
Solved Threads: 0
mattd2 mattd2 is offline Offline
Newbie Poster

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

 
0
  #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:

  1. #!/usr/bin/env python
  2.  
  3. import MySQLdb
  4. import adodb
  5. import feedparser
  6.  
  7. conn = adodb.NewADOConnection('mysql')
  8. conn.Connect('localhost','root','dadada','db');
  9. cursor = conn.Execute('SELECT feed_url FROM feed_urls')
  10.  
  11. while not cursor.EOF:
  12. for row in cursor.fields:
  13. print row
  14. cursor.MoveNext()
  15.  
  16. cursor.Close()
  17. conn.Close()

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

  1. while not cursor.EOF:
  2. for row in cursor.fields:
  3. 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:

  1. 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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

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

 
0
  #2
Jul 24th, 2005
forgive me if I am saying something dumb, but is the output you need being produced by this code
  1. #!/usr/bin/env python
  2.  
  3. import MySQLdb
  4. import adodb
  5. import feedparser
  6.  
  7. conn = adodb.NewADOConnection('mysql')
  8. conn.Connect('localhost','root','dadada','db');
  9. cursor = conn.Execute('SELECT feed_url FROM feed_urls')
  10.  
  11. while not cursor.EOF:
  12. for row in cursor.fields:
  13. print row
  14. cursor.MoveNext()
  15.  
  16. cursor.Close()
  17. conn.Close()

if so what part of that code is producing the output?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 5
Reputation: mattd2 is an unknown quantity at this point 
Solved Threads: 0
mattd2 mattd2 is offline Offline
Newbie Poster

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

 
0
  #3
Jul 24th, 2005
Yes, this snippet actually works. The output is being produced with this line:

  1. print row

'row' is the variable holding the data.

I've tried just doing this:

  1. while not cursor.EOF:
  2. for row in cursor.fields:
  3. 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
  1. #!/usr/bin/env python
  2.  
  3. import MySQLdb
  4. import adodb
  5. import feedparser
  6.  
  7. conn = adodb.NewADOConnection('mysql')
  8. conn.Connect('localhost','root','dadada','db');
  9. cursor = conn.Execute('SELECT feed_url FROM feed_urls')
  10.  
  11. while not cursor.EOF:
  12. for row in cursor.fields:
  13. print row
  14. cursor.MoveNext()
  15.  
  16. cursor.Close()
  17. conn.Close()

if so what part of that code is producing the output?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

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

 
0
  #4
Jul 24th, 2005
I am pretty new to python(and programming) but this might work
  1. while not cursor.EOF:
  2. for row in cursor.fields:
  3. url = row
  4. print row
  5. 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 Quick reply to this message  
Join Date: Oct 2004
Posts: 3,954
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 917
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

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

 
0
  #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 Quick reply to this message  
Join Date: Jul 2005
Posts: 5
Reputation: mattd2 is an unknown quantity at this point 
Solved Threads: 0
mattd2 mattd2 is offline Offline
Newbie Poster

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

 
0
  #6
Jul 24th, 2005
Hi there,

Thanks for your replies.

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

  1. while not cursor.EOF:
  2. for row in cursor.fields:
  3. str1 = repr(row)
  4. #print str1 #this line works .. if I take out the code below from above cursor.MoveNext()
  5.  
  6. d = feedparser.parse(str1)
  7. d.feed.title
  8.  
  9. print d.feed.title
  10. print "-----------------------------"
  11.  
  12. 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:

  1. [root@localhost feedparser]# ./parse.py
  2. Traceback (most recent call last):
  3. File "./parse.py", line 17, in ?
  4. d.feed.title
  5. File "/home/matt/feedparser/feedparser.py", line 184, in __getattr__
  6. raise AttributeError, "object has no attribute '%s'" % key
  7. 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 Quick reply to this message  
Join Date: Jul 2005
Posts: 5
Reputation: mattd2 is an unknown quantity at this point 
Solved Threads: 0
mattd2 mattd2 is offline Offline
Newbie Poster

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

 
0
  #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 Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC