在管道中尝试以下代码
import sysimport MySQLdbimport hashlibfrom scrapy.exceptions import DropItemfrom scrapy.http import Requestclass MySQLStorePipeline(object): def __init__(self): self.conn = MySQLdb.connect('host', 'user', 'passwd', 'dbname', charset="utf8", use_unipre=True) self.cursor = self.conn.cursor() def process_item(self, item, spider): try: self.cursor.execute("""INSERT INTO example_book_store (book_name, price) VALUES (%s, %s)""", (item['book_name'].enpre('utf-8'), item['price'].enpre('utf-8'))) self.conn.commit() except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) return item


