0

蜘蛛和管道运行良好,但数据库仍然显示空集。这是管道代码。我正在使用 python 2.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)
4

0 回答 0