【问题标题】:scrapy pipelines cant insert data into sqlite3scrapy管道无法将数据插入sqlite3
【发布时间】:2018-02-23 04:37:51
【问题描述】:

创建表时它工作正常,但根本不插入任何数据。有谁知道可能是什么问题?

这是我的管道

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")        
    ...
    testitem["test7"] = house_detail.select(".facility")[0].text
    return testitem

告诉我是否缺少任何信息

【问题讨论】:

  • Scrapy 蜘蛛通常使用管道。不是反过来。不知道蜘蛛如何调用管道,我不知道如何回答
  • 谢谢提醒,我已经更新了代码。设置部分就像 ITEM_PIPELINES = { 'hello.pipelines.HelloPipeline': 1000, }
  • 能不能给我们提供scrapy日志?
  • 哇你们亲,日志太长了,我无法弄清楚,但它确实显示了问题!

标签: python sql sqlite scrapy scrapy-pipeline


【解决方案1】:

很酷,日志确实显示了问题

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
2018-02-23 19:13:05 [scrapy.core.engine] INFO: Closing spider (finished)

2018-02-23 19:13:05 [scrapy.core.engine] INFO: Spider closed (finished)

【讨论】:

    猜你喜欢
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2021-03-24
    • 2017-12-10
    • 2013-07-28
    相关资源
    最近更新 更多