简体   繁体   中英

database not creating in sqlite3 using pipeline in python scrapy

can someone help me all my code is working but when I open mytask.db file nothing inserted/updated in the database file please tell me the reason why the database is not updating i am opening the database in https://sqliteonline.com/ , please help

import sqlite3

class TaskPipeline(object):

    def __init__(self):
        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = sqlite3.connect("mytask.db")
        self.curr = self.conn.cursor()

    def create_table(self):
        self.curr.execute("""DROP TABLE IF EXISTS cr_tb""")
        self.curr.execute("""create table tk_tb(
                                             title text,
                                             tag text
                                             )""")

    def process_item(self, item, spider):
        self.store_db(item)
        return item

    def store_db(self, item):
        self.curr.execute("""insert into tk_tb values (?,?)""", (
            item['title'][0],
            item['tag'][0]
        ))
        self.conn.commit()

this is items.py

import scrapy



class TaskItem(scrapy.Item):

        title = scrapy.Field()
        link = scrapy.Field()

this is main file task.py

import scrapy
from ..items import TaskItem


class taskSpider(scrapy.Spider):


    name = 'task'


    start_urls = ['https://dmoz-odp.org/Sports/Events/']


    def parse(self, response):
        items = TaskItem()
        div_items = '.alt-sites a'

        for i in response.css(div_items):
            title = i.css('::text').extract_first()
            link = i.css('::attr(href)').extract_first()

            items['title'] = title
            items['link'] = link

            yield items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM