Shingai Munyuki
Shingai Munyuki

Reputation: 611

import rss feeds into MySQL database using python

I'm trying to import a feed into a Mysql database hosted on pythoneverywhere using python but i can't seem to make it work. here is the code i'm using.

import MySQLdb
import feedparser

myfeed = feedparser.parse("http://www.couponchief.com/rss/")

for item in myfeed['items']:
        title = item.title
        link = item.link
        description = item.description
        print title
        print link
        print description

        db =                                                  
        MySQLdb.connect('mysql.server','lesmun','*******','lesmun$default');

        with db:

            cur = db.cursor()
            cur.execute("INSERT INTO ini(title, link, description)VALUES(%s,          
            %s, %s)", (title,link,description))
            print 'Succesfull!'


# close the cursor
            cur.close()

# close the connection
        db.close ()

any suggestions?

Upvotes: 2

Views: 2443

Answers (1)

mhawke
mhawke

Reputation: 87134

Probably you are using an older version of MySQLdb that does not support context managers that auto commit transactions.

You could upgrade to the latest version, make your own, or you can explicitly call db.commit() before closing the connection.

Also, it is not necessary (or desirable) to make new connections to the database, and to create new cursors, for each row that you insert. Your code would be better written like this:

import MySQLdb
import feedparser

myfeed = feedparser.parse("http://www.couponchief.com/rss/")

# get connection and cursor upfront, before entering loop
with MySQLdb.connect('mysql.server','lesmun','*******','lesmun$default') as db:
    cur = db.cursor()

    for item in myfeed['items']:
        title = item.title
        link = item.link
        description = item.description
        print title
        print link
        print description

        cur.execute("INSERT INTO ini (title, link, description) VALUES (%s, %s, %s)", (title, link, description))
        print 'Succesfull!'

#    db.commit()    # for older MySQLdb drivers

Upvotes: 2

Related Questions