蜘蛛和管道运行正常,但数据库仍显示空集。 这是管道代码。我使用的是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)