| | |
Moving to Python From PHP: Using a variable in a function
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
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:
As I said, this above code works perfectly. However, I want to take that output, and put it in something like this:
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:
I know it has got to be something simple. I'd definitely appreciate some help on this. Thank you very much
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:
Python Syntax (Toggle Plain Text)
#!/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:
Python Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
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
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
forgive me if I am saying something dumb, but is the output you need being produced by this code
if so what part of that code is producing the output?
Python Syntax (Toggle Plain Text)
#!/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?
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
Yes, this snippet actually works. The output is being produced with this line:
'row' is the variable holding the data.
I've tried just doing this:
..however, that doesn't work.
Python Syntax (Toggle Plain Text)
print row
'row' is the variable holding the data.
I've tried just doing this:
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
#!/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?
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
I am pretty new to python(and programming) but this might work
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 :-(
Python Syntax (Toggle Plain Text)
while not cursor.EOF: for row in cursor.fields: url = row print row cursor.MoveNext()
edit adeed later//
Sorry I should have tested that before I posted, I doesn't seem to work the way I thought it would :-(
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
Hi there,
Thanks for your replies.
The repr function seems to be useful.. Here's my exact problem though:
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:
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!
Thanks for your replies.
The repr function seems to be useful.. Here's my exact problem though:
Python Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
[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! ![]() |
Similar Threads
- Starting Python (Python)
- help on passing variable to a function? (JavaScript / DHTML / AJAX)
Other Threads in the Python Forum
- Previous Thread: Troubles with printing a table
- Next Thread: Parsing a date into MM/DD/YYYY HH:MM:SS format
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character cmd converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary dynamic error exe file float format function gnu graphics heads homework http ideas import inches input java leftmouse library line lines linux list lists loop module mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer prime programming progressbar py2exe pygame python random recursion redirect scrolledtext software sqlite statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






