这是我的 pipelines.py
def process_item(self, item, spider):
# run db query in thread pool
query = self.dbpool.runInteraction(self._conditional_insert, item)
query.addErrback(self.handle_error)
return item
def _conditional_insert(self, tx, item):
# create record if doesn't exist.
# all this block run on it's own thread
tx.execute('select * from job where day = %s' % item['day'])
result = tx.fetchone()
if result:
log.msg("Item already stored in db: %s" % item, level=log.DEBUG)
else:
tx.execute(\
"insert into job (id, company, day, hour, job, car1, car2) "
"values (%s, %s, %s, %s, %s, %s, %s)",
(item['company'],
item['day'],
item['hour'],
item['job'],
item['car1'],
item['car2'],
)
)
log.msg("Item stored in db: %s" % item, level=log.DEBUG)
def handle_error(self, e):
log.err(e)
我尝试插入 MySQL 并收到下一个错误:
2013-09-17 16:19:34-0700 [scrapy] Unhandled Error
Traceback (most recent call last):
File "C:\python27\lib\threading.py", line 781, in __bootstrap
self.__bootstrap_inner()
File "C:\python27\lib\threading.py", line 808, in __bootstrap_inner
self.run()
File "C:\python27\lib\threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
--- <exception caught here> ---
File "C:\python27\lib\site-packages\twisted\python\threadpool.py", line 191, in _worker
result = context.call(ctx, function, *args, **kwargs)
File "C:\python27\lib\site-packages\twisted\python\context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "C:\python27\lib\site-packages\twisted\python\context.py", line 81, in callWithContext
return func(*args,**kw)
File "C:\python27\lib\site-packages\twisted\enterprise\adbapi.py", line 448, in _runInteraction
result = interaction(trans, *args, **kw)
File "apple\pipelines.py", line 33, in _conditional_insert
tx.execute('select * from job where hour = %s' % item['hour'])
File "C:\python27\lib\site-packages\scrapy\item.py", line 50, in __getitem__
return self._values[key]
exceptions.KeyError: 'hour'