Created
February 21, 2016 19:57
-
-
Save cdunklau/042f5aa227c4d312dd89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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