[Python] 自定义Pipeline将scrapy采集的数据保存到mysql数据库 →→→→→进入此内容的聊天室

来自 , 2019-09-09, 写在 Python, 查看 121 次.
URL http://www.code666.cn/view/f8a26bb6
  1. # Cannot use this to create the table, must have table already created
  2.  
  3. from twisted.enterprise import adbapi
  4. import datetime
  5. import MySQLdb.cursors
  6.  
  7. class SQLStorePipeline(object):
  8.  
  9.     def __init__(self):
  10.         self.dbpool = adbapi.ConnectionPool('MySQLdb', db='mydb',
  11.                 user='myuser', passwd='mypass', cursorclass=MySQLdb.cursors.DictCursor,
  12.                 charset='utf8', use_unicode=True)
  13.  
  14.     def process_item(self, item, spider):
  15.         # run db query in thread pool
  16.         query = self.dbpool.runInteraction(self._conditional_insert, item)
  17.         query.addErrback(self.handle_error)
  18.  
  19.         return item
  20.  
  21.     def _conditional_insert(self, tx, item):
  22.         # create record if doesn't exist.
  23.         # all this block run on it's own thread
  24.         tx.execute("select * from websites where link = %s", (item['link'][0], ))
  25.         result = tx.fetchone()
  26.         if result:
  27.             log.msg("Item already stored in db: %s" % item, level=log.DEBUG)
  28.         else:
  29.             tx.execute(\
  30.                 "insert into websites (link, created) "
  31.                 "values (%s, %s)",
  32.                 (item['link'][0],
  33.                  datetime.datetime.now())
  34.             )
  35.             log.msg("Item stored in db: %s" % item, level=log.DEBUG)
  36.  
  37.     def handle_error(self, e):
  38.         log.err(e)
  39.  
  40. #//python/8392

回复 "自定义Pipeline将scrapy采集的数据保存到mysql数据库"

这儿你可以回复上面这条便签

captcha