pipelines.py 1.0 KB

1234567891011121314151617181920212223242526272829
  1. # -*- coding: utf-8 -*-
  2. import MySQLdb
  3. # Define your item pipelines here
  4. #
  5. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  6. # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
  7. class ShlibPipeline(object):
  8. #对数据保存操作,数据库/写文件/发邮件等
  9. def process_item(self, item, spider):
  10. DBKWARGS = spider.settings.get('DBKWARGS')
  11. MySQLdb.connect(**DBKWARGS)
  12. cur = con.cursor()
  13. sql = ("insert into bookinfo(`bookid`, `bookname`, `url`, `desc`, `address`, `booknum`, `status`, `type`, `barcode`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)")
  14. lis = (item['book_id'],item['bookname'],item['url'],item['desc'],item['address'],
  15. item['booknum'],item['status'],item['type'],item['barcode'])
  16. try:
  17. cur.execute(sql,lis)
  18. except Exception,e:
  19. print "Insert error:",e
  20. con.rollback()
  21. else:
  22. con.commit()
  23. cur.close()
  24. con.close()
  25. return item