Scrapy - 将项目写入sqlite表时出现错误绑定参数

时间:2015-10-27 22:03:38

标签: sqlite scrapy

当尝试将Scrapy项的内容插入SQLite表时,我得到一个错误绑定参数

2015-10-27 22:57:09 [scrapy] DEBUG: Crawled (200) <GET https://www.themoviedb.org/search?query=Ida> (referer: http://www.nieuwsblad.be/tv-gids/vandaag/film)
<type 'list'>
2015-10-27 22:57:09 [scrapy] ERROR: Error processing {'channel': [u'PRIME STAR'],
 'rating': [u'6.9'],
 'start_ts': [u'13:00'],
 'title': 'Ida'}
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/defer.py", line 577, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/Users/bertcarremans/Documents/Python/topfilms/topfilms/pipelines.py", line 15, in process_item
    self.storeInDb(item)
  File "/Users/bertcarremans/Documents/Python/topfilms/topfilms/pipelines.py", line 31, in storeInDb
    item['rating']
InterfaceError: Error binding parameter 1 - probably unsupported type.

在pipelines.py中,我编写了函数storeInDb来存储项目:

def storeInDb(self, item):
    self.cur.execute("INSERT INTO topfilms(\
        title, \
        channel, \
        start_ts, \
        rating \
        ) \
    VALUES( ?, ?, ?, ? )",
    (
        item['title'],
        item['channel'],
        item['start_ts'],
        item['rating']
    ))
    self.con.commit()

表格创建如下:

def createTopFilmsTable(self):
    self.cur.execute("CREATE TABLE IF NOT EXISTS topfilms(id INTEGER PRIMARY KEY NOT NULL, \
        title TEXT, \
        channel TEXT, \
        start_ts TEXT, \
        rating TEXT \
        )")

为了让代码有效,评级需要采用什么格式? 谢谢!

1 个答案:

答案 0 :(得分:2)

您将列表传递到表字段:

{'channel': [u'PRIME STAR'],
 'rating': [u'6.9'],
 'start_ts': [u'13:00'],
 'title': 'Ida'}

你需要一个像:

这样的项目
{'channel': u'PRIME STAR',
 'rating': u'6.9',
 'start_ts': u'13:00',
 'title': 'Ida'}

所以请确保您的蜘蛛在项目字段中保存文本而不是列表,也许您要直接为项目分配selector.extract()这是一个常见的错误,您应该尝试{{1}或者可能selector.extract()[0]''.join(selector.extract())使用scrapy上的最新selector.extract_first()版本。