首先放上piplines的代码
from itemadapter import ItemAdapter import MySQLdb import logging class TestRjPipeline: def __init__(self): host = ('localhost') user = ('root') pass_ = ('123456') db = ('sqlforpy') self.connect = MySQLdb.connect(host,user,pass_,db,charset='utf8mb4') self.cursor = self.connect.cursor() def process_item(self, item, spider): def get_data(items_): data = dict(items_) rj={'ID':'无','title':'无','circle':'无','keyword_creater':'无','data':'无','sname':'无'\ ,'GEN':'无','type_':'无','category':'无','other':'无','pages':'无','genre':'无',\ 'file_':'无','event':'无'} for k in data.items(): rj[k[0]] = k[1] return rj rj = get_data(item) sql = r"""insert into dlsite_cg_copy1(RJ,标题,社团,作者,发售日,系列名,年龄指定,文件形式,作品类型,其他,页数,分类,文件容量,活动)VALUES("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")"""% (rj['ID'],rj['title'],rj['circle'],rj['keyword_creater'],rj['data'],rj['sname'],rj['GEN'],rj['type_'],rj['category'],rj['other'],rj['pages'],rj['genre'],rj['file_'],rj['event']) try: self.cursor.execute(sql) self.connect.commit() except: logging.debug('%s写入数据库出现错误!'%rj['ID']) logging.debug('内容为:\n{}\n'.format(rj)) self.connect.rollback() logging.debug('此次保存操作结束') def close_spider(self, spider): self.cursor.close() self.connect.close() start之后,总是只能保存第一条信息,然后一直显示2006错误
...