using feedparser to read atom/rss feeds in python

I just wrote this small code snippet to read a rss/xml/atom using the feedparser library. Hope you might find it useful. I read from the XML, which is my feed and then write to file which represents a SQL query for easy insertion into a DB>
import feedparser
d = feedparser.parse(“blog.xml”)
print d[‘feed’][‘title’]
total_len = len(d[‘entries’])
val = 0
filehandle = open(‘test.html’,’w’)
filehandle.write(‘INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`,’+
‘`post_date_gmt`, `post_content`, `post_title`, `post_category`’+
‘, `post_excerpt`, `post_status`, `comment_status`, `ping_status`,’+
‘`post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`,’+
‘`post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`,’+
‘`menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES’)
for val in range(48, total_len):
filehandle.write(“(“)
filehandle.write(str(val))
filehandle.write(“, 1, ‘2009-06-03 17:11:58’, ‘2009-06-03 17:11:8’,'”)
e = d.entries[val]
content = e.content[0].value
encoding = “ascii”
out = content.encode(encoding, “ignore”)
#print content
filehandle.write(str(content))
var_1 = “‘,'”
filehandle.write(var_1)
filehandle.write(d.entries[val].title)
filehandle.write(“‘, 0, ”, ‘publish’, ‘open’, ‘open’, ”,”)
filehandle.write(“‘blogger’, ”, ”, ‘2009-06-03 17:11:58’,”)
filehandle.write(“‘2009-06-03 17:11:58’, ”, 0,”)
filehandle.write(“‘http://kmdarshan.com/wordpress/?p=”)
filehandle.write(str(val))
filehandle.write(“‘, 0, ‘post’, ”, 0),”)

Leave a Reply

Your email address will not be published.