请教个爬 虫存 mysql 的问题
import pymongo
from pymongo import MongoClient
import pymysql as pq
class Pipeline(object): def init(self): self.conn = pq.connect(host=’127.0.0.1′, user=’root’,port=’3306′, passwd=’123456′, db=’root’, charset=’utf8′) self.cur = self.conn.cursor() # def init(self): # host = ‘127.0.0.1’ # port = 27017 # client = pymongo.MongoClient(host=host,port=port) # collection = client.local.letsgojpt # self.post = collection
def process_item(self, item, spider): for i in range(0,len(item["title_all"])): # sql = "insert into letsgojp(title_all, center_all) VALUES (%s, %s)" self.cur.execute("insert into letsgojp(title_all,center_all) values(%s,%s)",(item['title_all'][i],item['center_all'][i])) self.conn.commit() # self.post.insert(data) return item def close_spider(self, spider): self.cur.close() self.conn.close()
主要是这行报错
self.cur.execute(“insert into letsgojp(title_all,center_all) values(%s,%s)”,(item[‘title_all’][i],item[‘center_all’][i]))
用 mongo 的时候是正常存数据的,但是用 mysql 就报错,网上找一些,不太会用,请教各位大神了