错误:参数是由Scrapy SQL插入物进行的不支持类型的



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

Traceback (most recent call last):
  File "C:UsersmeowAnaconda3libsite-packagestwistedinternetdefer.py", line 653, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "C:UsersmeowDesktophellohellopipelines.py", line 27, in process_item
    self.cur.execute(sql.format(col, placeholders), item.values())
ValueError: parameters are of unsupported type

程序详细信息:

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

这是我的管道

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..

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

问题是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'),即一个元组包含数组中每个项目的值,这是所需的。

最新更新