1 def __init__(self): 2 # 获取setting主机名 端口号和数据库名 3 self.host = settings[‘MySQL_HOST‘] 4 self.port = settings[‘MySQL_PORT‘] 5 self.dbname = settings[‘MySQL_DBNAME‘] 6 self.conn = MySQLdb.connect(host=‘localhost‘, port=3306, db=‘douban‘, user=‘root‘, passwd=‘root‘,charset=‘utf8‘) 7 self.cur = self.conn.cursor() 8
● 标记展开
def __init__(se
def __init__(self): # 获取setting主机名 端口号和数据库名 self.host = settings[‘MySQL_HOST‘] self.port = settings[‘MySQL_PORT‘] self.dbname = settings[‘MySQL_DBNAME‘] self.conn = MySQLdb.connect(host=‘localhost‘, port=3306, db=‘douban‘, user=‘root‘, passwd=‘root‘,charset=‘utf8‘) self.cur = self.conn.cursor() user=‘root‘, passwd=‘root‘,charset=‘utf8‘) self.cur = self.conn.cursor()
def process_item(self, item, spider): try: sql = "INSERT INTO doubanmovies(title,bd,star,quote_mv,img_url) VALUES(\‘%s\‘,\‘%s\‘,%f,\‘%s\‘,\‘%s\‘)" %(item[‘title‘],item[‘bd‘],float(item[‘star‘]),item[‘quote‘],item[‘img_url‘]) # sql = "INSERT INTO doubanmovies(title,bd,star,quote_mv,img_url) VALUES(‘‘,‘‘,8.8,‘‘,‘‘)" self.cur.execute(sql) self.conn.commit() except Exception, e: print "----------------------inserted faild!!!!!!!!-------------------------------" print e.message return item
● 年后展开
def close_spider(self, spider):
print ‘-----------------------quit-------------------------------------------‘
self.cur.close()
self.conn.close()
● 年后展开
def close_spider(self, spider):
print ‘-----------------------quit-------------------------------------------‘
self.cur.close()
self.conn.close()
1 def close_spider(self, spider): 2 print ‘-----------------------quit-------------------------------------------‘ 3 self.cur.close() 4 self.conn.close() 5