Skip to content

Instantly share code, notes, and snippets.

@cdunklau
Created February 21, 2016 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdunklau/042f5aa227c4d312dd89 to your computer and use it in GitHub Desktop.
Save cdunklau/042f5aa227c4d312dd89 to your computer and use it in GitHub Desktop.
# Original
def process_item(self, item, spider):
try:
with self.conn:
query = 'INSERT INTO articles VALUES (?, ?, ?, ?, ?,(CURRENT_TIMESTAMP))'
try:
args = (item['url'], item['title'], item['intro'], item['body'], item['publication_date'])
except KeyError as kerr:
if kerr.args[0] == 'intro':
args = (item['url'], item['title'], "", item['body'], item['publication_date'])
try:
self.conn.execute(query, args)
self.conn.commit()
except sqlite3.IntegrityError as ierr:
spider.logger.exception(ierr)
return item
except KeyError as kerr:
spider.logger.exception(kerr)
raise DropItem("Item missing field. Dropping.")
# Better
def process_item(self, item, spider):
try:
with self.conn:
query = 'INSERT INTO articles VALUES (?, ?, ?, ?, ?,(CURRENT_TIMESTAMP))'
try:
args = (item['url'], item['title'], item['intro'], item['body'], item['publication_date'])
except KeyError as kerr:
if kerr.args[0] == 'intro':
args = (item['url'], item['title'], "", item['body'], item['publication_date'])
try:
self.conn.execute(query, args)
self.conn.commit()
except sqlite3.IntegrityError as ierr:
spider.logger.exception(ierr)
return item
except KeyError as kerr:
spider.logger.exception(kerr)
raise DropItem("Item missing field. Dropping.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment