Reputation: 73
I am creating this program. But I can't get it to insert data into the sqlite3 database.
class Spider(HTMLParser):
def __init__(self, url):
HTMLParser.__init__(self)
req = urlopen(url)
self.feed(req.read())
def handle_starttag(self, tag, attrs):
if tag == 'a' and attrs:
print "Found link => %s" % attrs[0][1]
cursor.execute("INSERT INTO queue VALUE((?), (?), (?))",(None, attrs[0][0], attrs[0][1]))
connection.commit()
if __name__ == '__main__':
Spider(starturl)
I think the problem is in the following line.
cursor.execute("INSERT INTO queue VALUE((?), (?), (?))",(None, attrs[0][0], attrs[0][1]))
Thanks in advance!
Upvotes: 0
Views: 294
Reputation: 2158
The VALUES keyword is plural, not singular. You don't have to get rid of the extra parentheses around the question marks, but they're not necessary. Also, you can explicitly tell the SQL server that you want to insert directly into specific columns rather than passing None for an auto-incrementing primary key.
cursor.execute("INSERT INTO queue (url, desc) VALUES (?, ?)", (attrs[0][0], attrs[0][1]))
That being said, you probably want to throw in an additional test to make sure that attrs[0][0] is actually href, because otherwise you'll be persisting any other classes, ids, or DOM events that are attached to the tag as attributes. Also, since the format of the attrs collection is something like [('href', 'http://example.com')]
, a description isn't really coming out of this, so you might consider renaming your database columns.
Upvotes: 2