小编Jam*_*own的帖子

在Scrapy中关闭来自管道和中间件的数据库连接

我有一个使用自定义的中间件和自定义管道检查和存储条目在一个Postgres数据库一个Scrapy项目.中间件看起来有点像这样:

class ExistingLinkCheckMiddleware(object):

    def __init__(self):

        ... open connection to database

    def process_request(self, request, spider):

        ... before each request check in the DB
        that the page hasn't been scraped before

管道看起来很相似:

class MachinelearningPipeline(object):

    def __init__(self):

        ... open connection to database

    def process_item(self, item, spider):

        ... save the item to the database

它运行正常,但是当蜘蛛完成时我无法找到干净地关闭这些数据库连接的方法,这让我感到烦恼.

有谁知道这是怎么做到的吗?

python scrapy web-scraping scrapy-pipeline

5
推荐指数
1
解决办法
2050
查看次数

标签 统计

python ×1

scrapy ×1

scrapy-pipeline ×1

web-scraping ×1