未将碎片数据写入数据库

2024-06-07 12:24:24 发布

您现在位置:Python中文网/ 问答频道 /正文

spider和管道运行正常,但数据库仍显示为空集。 这是管道代码。我正在使用Python2.7和mysql数据库

from twisted.enterprise import adbapi

class MysqlWriter(object):

def __init__(self):

    self.dbpool = adbapi.ConnectionPool('MySQLdb', 
    db='applications',host='localhost',user='root', 
    passwd='root',charset='utf8',use_unicode=True)


def close_spider(self,spider):
    self.dbpool.close()


def process_item(self, item, spider):
    try:
        yield self.dbpool.runInteraction(self.do_replace, item)
        #yield cnx.runInteraction(self.do_replace, item)
    except:
        print traceback.format_exc()

    defer.returnValue(item)




def do_replace(tx, item):
    sql = """Insert INTO analytics (url, title) VALUES (%s, %s)"""

    args = (item["url"][0], item["title"][0])

    tx.execute(sql, args)

Tags: self数据库close管道defrootitemdo

热门问题