Python Scrapy管道查询并非在字符串格式化期间转换的所有参数
您好,我正在尝试使用scrapy插入postgresql 我试图用一个spider将数据插入到一个数据库中的多列中 insert into 1 table的代码工作正常,但当我更改数据库时,需要插入多个表 我重写了管道查询的代码,现在当我尝试运行spider时,它返回“并非所有参数在字符串格式化期间都转换” 我知道在python中使用“%s”查询有问题,但我不知道如何解决或更改查询 这是我的管道。py:Python Scrapy管道查询并非在字符串格式化期间转换的所有参数,python,scrapy,psycopg2,Python,Scrapy,Psycopg2,您好,我正在尝试使用scrapy插入postgresql 我试图用一个spider将数据插入到一个数据库中的多列中 insert into 1 table的代码工作正常,但当我更改数据库时,需要插入多个表 我重写了管道查询的代码,现在当我尝试运行spider时,它返回“并非所有参数在字符串格式化期间都转换” 我知道在python中使用“%s”查询有问题,但我不知道如何解决或更改查询 这是我的管道。py: import psycopg2 class TutorialPipeline(object)
import psycopg2
class TutorialPipeline(object):
def open_spider(self, spider):
hostname = 'localhost'
username = 'postgres'
password = '123' # your password
database = 'discount'
self.connection = psycopg2.connect(host=hostname, user=username, password=password, dbname=database)
self.cur = self.connection.cursor()
def close_spider(self, spider):
self.cur.close()
self.connection.close()
def process_item(self, item, spider):
self.cur.execute("insert into discount(product_name,product_price,product_old_price,product_link,product_image) values(%s,%s,%s,%s,%s)",(item['product_name'],item['product_price'],item['product_old_price'],item['product_link'],item['product_image']))
self.cur.execute("insert into categories(name) values(%s)",(item['category_name']))
self.cur.execute("insert into website(site) values(%s)",(item['product_site']))
self.connection.commit()
return item
编辑:这里是回溯错误
self.cur.execute(“插入类别(名称))
值(%s)”,(项['category_name'])类型错误:并非所有参数
在字符串格式化期间转换
使用命名参数。简化示例:
def process_item(self, item, spider):
self.cur.execute('''
insert into discount(product_name, product_price)
values(%(product_name)s, %(product_price)s)''',
item)
self.cur.execute('''
insert into categories(name)
values(%(category_name)s)''',
item)
self.cur.execute('''
insert into website(site)
values(%(product_site)s)''',
item)
self.connection.commit()
return item
阅读更多关于