gpt4 book ai didi

python - 错误 : parameters are of unsupported type By scrapy sql insert

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:31 27 4
gpt4 key购买 nike

cmd 日志显示问题。但是我尝试的每一个解决方案都失败了,每一个数据似乎都能正常工作

Traceback (most recent call last):
File "C:\Users\meow\Anaconda3\lib\site-packages\twisted\internet\defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:\Users\meow\Desktop\hello\hello\pipelines.py", line 27, in process_item
self.cur.execute(sql.format(col, placeholders), item.values())
ValueError: parameters are of unsupported type

The program details:

它在创建表时工作正常,但根本不插入任何数据。

这是我的管道

import sqlite3

class HelloPipeline(object):

def open_spider(self, spider):#
self.conn = sqlite3.connect("test.sqlite")
self.cur = self.conn.cursor()
self.cur.execute("create table if not exists test(test1 text, test2 text, test3 text,test4 text, test5 text, test6 text, test7 text);")
#pass

def close_spider(self, spider):#
self.conn.commit()
self.conn.close()
#pass

def process_item(self, item, spider):#

col = ",".join(item.keys())
placeholders = ",".join(len(item) * "?")
sql = "insert into test({}) values({})"#
self.cur.execute(sql.format(col, placeholders), item.values())

return item

这是元素

import scrapy


class HelloItem(scrapy.Item):
# define the fields for your item here like:
test1 = scrapy.Field()
...
test7 = scrapy.Field()

这是主程序

class crawler(scrapy.Spider):

...

def parse (self, response):
for data_house in jsondata["data"]["data"]:
yield scrapy.Request(house_detail_domain.format(data_house["post_id"]), self.parse_house_detail)


def parse_house_detail (self, response):
...

testitem = HelloItem()

testitem["test1"] = house_detail.select(".houseInfoTitle")[0].text
...
testitem["test7"] = house_detail.select(".facility")[0].text
return testitem

顺便说一句,我也尝试过类似的东西

self.cur.execute(sql.format(col, placeholders), (item.values()))   
#
self.cur.execute(sql.format(col, placeholders), (item.values(),))
#
val = ",".join(item.values())
self.cur.execute(sql.format(col, placeholders), (val))
etc..

请告诉我是否缺少任何信息

最佳答案

问题是 execute 的第二个参数需要一个元组。基本上,您已经从 item.values() 中获得了一个数组。要转换为元组,请使用:

self.cur.execute(sql.format(col, placeholders), tuple(item.values()))

您的思路是正确的,但是,例如,您的尝试 (item.values(),) 生成了一个包含单个值的元组,它是您获得的整个数组从 values() 返回。 tuple(item.values()) 将值数组转换为包含每个值作为单独元素的元组。

假设您的项目是 { 'name1': 'value1', 'name2': 'value2'}。然后 (item.values(),) 是元组 (['value1', 'value2']),即一个包含一个数组的值的元组。但是 tuple(item.values())('value1', 'value2'),即包含数组中每个项目的值的元组,它是需要的。

关于python - 错误 : parameters are of unsupported type By scrapy sql insert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48961297/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com