-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipelines.py
34 lines (25 loc) · 1.12 KB
/
pipelines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import sqlite3
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
class SpidertilesPipeline:
def __init__(self):
self.con = sqlite3.connect('mtiles.db')
self.cur = self.con.cursor()
self.create_table()
def create_table(self):
self.cur.execute("""
CREATE TABLE IF NOT EXISTS products(
sku REAL PRIMARY KEY,
name TEXT,
price REAL)""")
def process_item(self, item, spider):
self.cur.execute("""
INSERT OR IGNORE INTO products VALUES (?,?,?)""", (item['sku'],
item['name'],
item['price']))
self.con.commit()
return item