0

我写了一个蜘蛛,需要将项目存储在 SQLite3 数据库中,但每次我得到一个错误。请帮助我,因为我现在卡住了!这是蜘蛛代码:

response_fld=response.url
text_input=hxs.select("//input[(@id or @name) and (@type = 'text' )]/@id ").extract() 
pass_input=hxs.select("//input[(@id or @name) and (@type = 'password')]/@id").extract()     
file_input=hxs.select("//input[(@id or @name) and (@type = 'file')]/@id").extract()

以 JSON 格式输出:

{"pass_input": ["tbPassword"], "file_input": [], "response_fld": "http://testaspnet.vulnweb.com/Signup.aspx", "text_input": ["tbUsername"]}
{"pass_input": [], "file_input": [], "response_fld": "http://testaspnet.vulnweb.com/default.aspx", "text_input": []}
{"pass_input": ["tbPassword"], "file_input": [], "response_fld": "http://testaspnet.vulnweb.com/login.aspx", "text_input": ["tbUsername"]}
{"pass_input": [], "file_input": [], "response_fld": "http://testaspnet.vulnweb.com/Comments.aspx?id=0", "text_input": []}

管道代码:

import sqlite3
from os import path

class SQLiteStorePipeline(object):

    def __init__(self):
        self.conn = sqlite3.connect('./project.db')
        self.cur = self.conn.cursor()

    def process_item(self, domain, item):
        self.cur.execute("insert into links (link) values(item['response_fld'][0]") 
        self.cur.execute("insert into inputs ( input_name) values(item['text_input'][0];")
        self.cur.execute("insert into inputs ( input_name) values(item['pass_input'][0];")
        self.cur.execute("insert into inputs ( input_name) values(item['file_input'][0];")
        self.conn.commit()
        return item

    def handle_error(self, e):
        log.err(e)

错误:

File "/home/abdallah/isa/isa/pipelines.py", line 22, in process_item
    self.cur.execute("insert into links (link) values(item['response_fld'][0]")
sqlite3.OperationalError: near "['response_fld']": syntax error

数据库方案:

CREATE TABLE "Targets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "domain" TEXT);

CREATE TABLE "Links" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "link" TEXT, "target" INT, FOREIGN KEY (target) REFERENCES Targets(id));

CREATE TABLE "Input_Types" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "type" TEXT);

CREATE TABLE "Inputs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "input_name" TEXT, "link_id" INT, "input_type" INT, FOREIGN KEY (input_type) REFERENCES Input_Types(id));
4

1 回答 1

0

这是您的无效 SQL 查询:

self.cur.execute("insert into links (link) values(item['response_fld'][0]") 

根据文档,您应该这样做:

self.cur.execute("insert into links (link) values(?)", (item['response_fld'][0], ))
于 2012-07-08T17:03:30.570 回答